Python 基于自定义函数聚合dataframe中的多列
下午好, 我已经尝试解决这个问题有一段时间了,任何帮助都将不胜感激 这是我的数据框:Python 基于自定义函数聚合dataframe中的多列,python,pandas,dataframe,group-by,summary,Python,Pandas,Dataframe,Group By,Summary,下午好, 我已经尝试解决这个问题有一段时间了,任何帮助都将不胜感激 这是我的数据框: Channel state rfq_qty A Done 10 B Tied Done 10 C Done 10 C Done 10 C Done 10 C Tied Done 10 B Done 10 B Done
Channel state rfq_qty
A Done 10
B Tied Done 10
C Done 10
C Done 10
C Done 10
C Tied Done 10
B Done 10
B Done 10
我想:
- 第一个筛选依据和
loc
- 并使用新列名称和函数的元组进行聚合
- 将
除以和百分比
总和
- 如有必要,通过
rfq\U数量
一种方法是使用单个
df.groupby.agg
并重命名列:
import pandas as pd
df = pd.DataFrame({'Channel': ['A', 'B', 'C', 'C', 'C', 'C', 'B', 'B'],
'state': ['Done', 'Tied Done', 'Done', 'Done', 'Done', 'Tied Done', 'Done', 'Done'],
'rfq_qty': [10, 10, 10, 10, 10, 10, 10, 10]})
agg_funcs = {'state': lambda x: x[x.str.contains('Done')].count(),
'rfq_qty': ['sum', lambda x: x.sum() / df['rfq_qty'].sum()]}
res = df.groupby('Channel').agg(agg_funcs).reset_index()
res.columns = ['Channel', 'state', 'rfq_qty', 'Percentage']
# Channel state rfq_qty Percentage
# 0 A 1 10 0.125
# 1 B 3 30 0.375
# 2 C 4 40 0.500
这不是最有效的方法,因为它依赖于非矢量化聚合,但是如果它适合您的用例,那么它可能是一个很好的选择。Hey Jezzrael。谢谢你。当我尝试对sum列进行sirt时,它无法从最大到最小排序。df.sort_值(['sum'],升序=False)@PeterLucas-只需删除
,升序=False
完美,列标题上的大小写问题。再次感谢@jpp-Hmmm,在我看来,如果OP先使用过滤,然后使用过滤后的df_Done
DataFrame,那就没问题了。@jpp-我同意,所以添加了注释df_Done=df[df['state'].str.contains('Done')]
df_Done = df[
(
df['state']=='Done'
)
|
(
df['state'] == 'Tied Done'
)
][['Channel','state','rfq_qty']]
df_Done['Percentage_Qty']= df_Done['rfq_qty']/df_Done['rfq_qty'].sum()
df_Done['Done_Trades']= df_Done['state'].count()
display(
df_Done[
(df_Done['Channel'] != 0)
].groupby(['Channel'])['Channel','Count of Done','rfq_qty','Percentage_Qty'].sum().sort_values(['rfq_qty'], ascending=False)
)
df_Done = df.loc[df['state'].isin(['Done', 'Tied Done']), ['Channel','state','rfq_qty']]
#if want filter all values contains Done
#df_Done = df[df['state'].str.contains('Done')]
#if necessary filter out Channel == 0
#mask = (df['Channel'] != 0) & df['state'].isin(['Done', 'Tied Done'])
#df_Done = df.loc[mask, ['Channel','state','rfq_qty']]
d = {('rfq_qty', 'sum'), ('Done_Trades','size')}
df = df_Done.groupby('Channel')['rfq_qty'].agg(d).reset_index()
df['Percentage'] = df['rfq_qty'].div(df['rfq_qty'].sum())
df = df.sort_values('rfq_qty')
print (df)
Channel Done_Trades rfq_qty Percentage
0 A 1 10 0.125
1 B 3 30 0.375
2 C 4 40 0.500
import pandas as pd
df = pd.DataFrame({'Channel': ['A', 'B', 'C', 'C', 'C', 'C', 'B', 'B'],
'state': ['Done', 'Tied Done', 'Done', 'Done', 'Done', 'Tied Done', 'Done', 'Done'],
'rfq_qty': [10, 10, 10, 10, 10, 10, 10, 10]})
agg_funcs = {'state': lambda x: x[x.str.contains('Done')].count(),
'rfq_qty': ['sum', lambda x: x.sum() / df['rfq_qty'].sum()]}
res = df.groupby('Channel').agg(agg_funcs).reset_index()
res.columns = ['Channel', 'state', 'rfq_qty', 'Percentage']
# Channel state rfq_qty Percentage
# 0 A 1 10 0.125
# 1 B 3 30 0.375
# 2 C 4 40 0.500