Groupby and aggregate using lambda functions

僤鯓⒐⒋嵵緔 提交于 2021-02-04 18:36:06

问题


I am trying to groupby-aggregate a dataframe using lambda functions that are being created programatically. This so I can simulate a one-hot encoder of the categories present in a column.

Dataframe:

df = pd.DataFrame(np.array([[10, 'A'], [10, 'B'], [20, 'A'],[30,'B']]),
                   columns=['ID', 'category'])

ID category
10 A
10 B
20 A
30 B

Expected result:

ID A B
10 1 1
20 1 0
30 0 1

What I am trying:

one_hot_columns = ['A','B']
lambdas = [lambda x: 1 if x.eq(column).any() else 0 for column in one_hot_columns]
df_g = df.groupby('ID').category.agg(lambdas)

Result:

ID A B
10 1 1
20 0 0
30 1 1

But the above is not quite the expected result. Not sure what I am doing wrong. I know I could do this with get_dummies, but using lambdas is more convenient for automation. Also, I can ensure the order of the output columns.


回答1:


Use crosstab:

pd.crosstab(df.ID, df['category']).reset_index()

Output:

category  ID  A  B
0         10  1  1
1         20  1  0
2         30  0  1



回答2:


You can use pd.get_dummies with Groupby.sum:

In [4331]: res = pd.get_dummies(df, columns=['category']).groupby('ID', as_index=False).sum()

In [4332]: res
Out[4332]: 
   ID  category_A  category_B
0  10           1           1
1  20           1           0
2  30           0           1

OR, use pd.concat with pd.get_dummies:

In [4329]: res = pd.concat([df, pd.get_dummies(df.category)], axis=1).groupby('ID', as_index=False).sum()

In [4330]: res
Out[4330]: 
   ID  A  B
0  10  1  1
1  20  1  0
2  30  0  1


来源:https://stackoverflow.com/questions/65119376/groupby-and-aggregate-using-lambda-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!