Pandas ValueError:只能比较相同标记的系列对象


df = pd.DataFrame([{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:34.160', 'Milliseconds':35974160 , 'RecordType':'ENTER', 'Value':36597.95},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:34.566', 'Milliseconds':35974566 , 'RecordType':'DELETE', 'Value':175.70},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:37.832', 'Milliseconds':35977832 , 'RecordType':'DELETE', 'Value':1093470.00},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:37.841', 'Milliseconds':35977841 , 'RecordType':'DELETE', 'Value':25799.34},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:38.846', 'Milliseconds':35978846 , 'RecordType':'ENTER', 'Value':2460.15},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:45.015', 'Milliseconds':35985015 , 'RecordType':'DELETE', 'Value':6731.00},
{'Instrument':'BHP', 'Date':'2012-04-18', 'Time':'09:59:47.024', 'Milliseconds':35987024 , 'RecordType':'OPEN', 'Value':np.nan}])```

我有上述数据帧。我的目标是从打开到打开之前 10 秒获取具有 RecordType DELETE 的值的总和。我尝试了以下代码:

opening_time = df[df.RecordType=='OPEN']
ten_seconds_before_open = opening_time['Milliseconds'] - 10*1000
delete_type = df[df.RecordType=='DELETE']
sum_delete = delete_type[delete_type.Milliseconds >= ten_seconds_before_open].Value.sum()
print(sum_delete)

但是,它返回ValueError: Can only compare identically-labeled Series objects。我可以知道最好的解决方案是什么吗?

事实上,我实际上有数百万行包含许多仪器和日期。我正在尝试编码以获取每个日期的每个工具的 DELETE 值的总和,

def sum_delete_type(df):
opening_time = df[df.RecordType=='OPEN']
ten_seconds_before_open = opening_time['Milliseconds'] - 10*1000
delete_type = df[df.RecordType=='DELETE']
sum_delete = delete_type[delete_type.Milliseconds >= ten_seconds_before_open].Value.sum()
return sum_delete
df.groupby(['Instrument', 'Date']).apply(sum_delete_type)

但它没有用。请帮忙。谢谢。

这个怎么样

opening_time = df[df.RecordType=='OPEN']
ten_seconds_before_open = opening_time['Milliseconds'] - 10*1000
delete_type = df[df.RecordType=='DELETE']
y=[]
for x in ten_seconds_before_open:
y.extend(delete_type[delete_type.Milliseconds >= x].index.tolist())
y=list(set(y))
delete_type[delete_type.index.isin(y)]['Milliseconds'].sum()

相关内容

  • 没有找到相关文章