每天的交易次数统计 pandas 数据框

2024-09-29 23:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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'

谢谢


Tags: 文件csv数据truedatesteporderbuy

热门问题