我有以下两个数据框:
df1
ticker date return high_low turnover
CRM 2017-01-03 0.018040 0.026957 5.722346e+08
MSFT 2017-01-03 -0.003344 0.011428 1.295037e+09
CRM 2017-01-04 0.024198 0.032646 6.762756e+08
MSFT 2017-01-04 -0.002881 0.010142 1.329482e+09
CRM 2017-01-05 -0.000275 0.015580 3.417927e+08
df2:
ticker date return high_low turnover
CRM 2017-01-03 0.018040 0.026957 5.722318e+08
MSFT 2017-01-03 -0.003344 0.011509 1.295037e+09
CRM 2017-01-04 0.024198 0.032575 6.761264e+08
MSFT 2017-01-04 -0.002881 0.010142 1.329480e+09
CRM 2017-01-05 -0.000275 0.015580 3.417930e+08
并且我有以下代码正在工作。 但是我想可以将其中的最后四行简化为一个。只需一行就可以对三列进行concat + groupby +应用。
def get_min_absvalue(values):
return min(values, key = abs)
#simplify the following 4 lines in 1?
consolidated_return=(pd.concat((df1,df2),ignore_index=True,sort=False).groupby(['date','ticker'])['return'].apply(lambda x: get_min_absvalue(x)).reset_index())
consolidated_high_low=(pd.concat((df1,df2),ignore_index=True,sort=False).groupby(['date','ticker'])['high_low'].apply(lambda x: get_min_absvalue(x)).reset_index())
consolidated_turnover=(pd.concat((df1,df2),ignore_index=True,sort=False).groupby(['date','ticker'])['turnover'].apply(lambda x: get_min_absvalue(x)).reset_index())
merged = consolidated_return.merge(consolidated_high_low, on=['date', 'ticker']).merge(consolidated_turnover, on=['date', 'ticker'])
有可能吗?
答案 0 :(得分:1)
df1
和df2
中的列:list
used_cols = ['return', 'high_low', 'turnover']
df_list = [pd.concat((df1, df2), ignore_index=True, sort=False).groupby(['date', 'ticker'])[v].apply(lambda x: min(x, key=abs)) for v in used_cols]
df_list
的内容:pandas.core.series.Series
df_list[0]
date ticker
2017-01-03 CRM 0.026957
MSFT 0.011428
2017-01-04 CRM 0.032575
MSFT 0.010142
2017-01-05 CRM 0.015580
Name: high_low, dtype: float64
concat
代替merge
:df_list
与concat
组合merged = pd.concat(df_list, axis=1).reset_index()
merged_new == merged_old
used_cols = ['return', 'high_low', 'turnover']
df_list = [pd.concat((df1, df2), ignore_index=True, sort=False).groupby(['date', 'ticker'])[v].apply(lambda x: min(x, key=abs)) for v in used_cols]
merged = pd.concat(df_list, axis=1).reset_index()
答案 1 :(得分:0)
考虑一个简单的min
集合调用:
agg_df = (pd.concat([df1,df2], ignore_index=True, sort=False)
.groupby(['date','ticker'], as_index=False)
.min()
)
print(agg_df)
# date ticker return high_low turnover
# 0 2017-01-03 CRM 0.018040 0.026957 5.722318e+08
# 1 2017-01-03 MSFT -0.003344 0.011428 1.295037e+09
# 2 2017-01-04 CRM 0.024198 0.032575 6.761264e+08
# 3 2017-01-04 MSFT -0.002881 0.010142 1.329480e+09
# 4 2017-01-05 CRM -0.000275 0.015580 3.417927e+08
print(merged.eq(agg_df))
# date ticker return high_low turnover
# 0 True True True True True
# 1 True True True True True
# 2 True True True True True
# 3 True True True True True
# 4 True True True True True
对于真实的绝对值最小聚合:
agg_df = (pd.concat([df1,df2], ignore_index=True, sort=False)
.groupby(['date','ticker'], as_index=False)['return', 'high_low', 'turnover']
.apply(lambda x: x.abs().min())
.reset_index()
)
print(agg_df)
# date ticker return high_low turnover
# 0 2017-01-03 CRM 0.018040 0.026957 5.722318e+08
# 1 2017-01-03 MSFT 0.003344 0.011428 1.295037e+09
# 2 2017-01-04 CRM 0.024198 0.032575 6.761264e+08
# 3 2017-01-04 MSFT 0.002881 0.010142 1.329480e+09
# 4 2017-01-05 CRM 0.000275 0.015580 3.417927e+08