我有以下代码:
import pandas as pd
import numpy as np
df = pd.read_csv('C:/test.csv')
df.drop(['SecurityID'],1,inplace=True)
Time = 1
trade_filter_size = 9
groupbytime = (str(Time) + "min")
df['dateTime_s'] = df['dateTime'].astype('datetime64[s]')
df['dateTime'] = pd.to_datetime(df['dateTime'])
df[str(Time)+"min"] = df['dateTime'].dt.floor(str(Time)+"min")
df['tradeBid'] = np.where(((df['tradePrice'] <= df['bid1']) & (df['isTrade']==1)), df['tradeVolume'], 0)
groups = df[df['isTrade'] == 1].groupby(groupbytime)
print("groups",groups.dtypes)
#THIS IS WORKING
df_grouped = (groups.agg({
'tradeBid': [('sum', np.sum),('downticks_number', lambda x: (x > 0).sum())],
}))
# creating a new data frame which is filttered
df2 = pd.DataFrame( df.loc[(df['isTrade'] == 1) & (df['tradeVolume']>=trade_filter_size)])
#recalculating all the bid/ask volume to be bsaed on the filter size
df2['tradeBid'] = np.where(((df2['tradePrice'] <= df2['bid1']) & (df2['isTrade']==1)), df2['tradeVolume'], 0)
df2grouped = (df2.agg({
# here is the problem!!! NOT WORKING
'tradeBid': [('sum', np.sum), lambda x: (x > 0).sum()],
}))
使用相同的功能tradeBid': [('sum', np.sum),('downticks_number', lambda x: (x > 0).sum())
。在第一次工作正常但是在新df中对过滤后的数据进行操作时会导致错误:
ValueError:downticks_number是一个未知的字符串函数
当我使用此代码代替解决上述问题时
'tradeBid': [('sum', np.sum), lambda x: (x > 0).sum()],
我收到此错误:
ValueError:无法组合转换和聚合操作
知道为什么我对同样的代码使用会得到不同的结果吗?
答案 0 :(得分:0)
因为有2个条件要匹配第二组,我通过创建一个用作过滤器的新列(带有两个过滤器)将过滤器移动到df中来解决这个问题。 对于groupby没有问题 订单是问题