我想从csv文件中检索一列,并将其作为数据帧中的索引。然而,我意识到我可能需要提前做另一步。在
csv看起来像这样
Date,Step,Order,Price
2011-01-10,Step,BUY,150
2011-01-10,Step,SELL,150
2011-01-13,Step,SELL,150
2011-01-13,Step1,BUY,400
2011-01-26,Step2,BUY,100
如果我打印数据帧,这是输出:
^{pr2}$然而,我想要的输出是告诉我每天每种类型的步骤有多少次买入/卖出。在
例如:
预期的数据帧和输出是:
Date Num-Buy-Sell
2011-01-10 2
2011-01-13 2
2011-01-16 1
这是我如何检索数据帧的代码
num_trasanctions_day = pd.read_csv(orders_file, parse_dates=True, sep=',', dayfirst=True)
num_trasanctions_day['Transactions'] = orders.groupby(['Date', 'Order'])
num_trasanctions_day['Date'] = num_trasanctions_day.index
我的第一个想法是将日期作为索引,但我想我需要计算每个日期有多少卖出/买入。在
错误
KeyError: 'Order'
谢谢
只使用
value_counts
编辑:如果你想把它分配回来,你正在寻找
^{pr2}$transform
,请修改你的预期输出。在相关问题 更多 >
编程相关推荐