求和Python Pandas中所有列的相似集合



我是Python Panda的初学者我在这个简历文件中收集交易时遇到问题,请帮忙如何编写命令来添加Pandas中所有类似货币对的大小例如,所有侧面的总尺寸=购买符号=btc irt

侧面符号尺寸

购买-btc irt-0.011

购买-btc irt-0.0045

sell-btc irt-0.0001

sell-btc irt-0.0001

对于csv文件中的列值求和,可以使用panda加载值并对值进行分组。

试试这个代码:

import pandas as pd
s = '''
side- symbol- size
buy- btc-irt- 0.011
buy- btc-irt- 0.0045
sell- btc-irt- 0.0001
sell- btc-irt- 0.0001
buy- eth-irt- 0.022
buy- eth-irt- 0.0046
sell- eth-irt- 0.0011
sell- eth-irt- 0.0021
'''.strip()
with open("data.csv",'w') as f: f.write(s) # write test file

#### main script ####
df = pd.read_csv('data.csv', sep=' ') # load data, columns separated by space
print('---- dataframe ----n',df)
gb = df.groupby(["side-","symbol-"]).sum() # group by two columns, sum of third column
print('nn---- grouped ----n',gb)

输出

---- dataframe ----
side-   symbol-    size
0   buy-  btc-irt-  0.0110
1   buy-  btc-irt-  0.0045
2  sell-  btc-irt-  0.0001
3  sell-  btc-irt-  0.0001
4   buy-  eth-irt-  0.0220
5   buy-  eth-irt-  0.0046
6  sell-  eth-irt-  0.0011
7  sell-  eth-irt-  0.0021

---- grouped ----
size
side- symbol-
buy-  btc-irt-  0.0155
eth-irt-  0.0266
sell- btc-irt-  0.0002
eth-irt-  0.0032

最新更新