ros*_*fun 3 python dataframe pandas
例如,我有一个名为的DataFrame a.我想要计算每一行的元素.
import numpy as np
a=pd.DataFrame({'A1':['financial','game','game'],'A2':['social','food','sport'],'A3':['social','sport','game']})
Run Code Online (Sandbox Code Playgroud)
Input:
A1 A2 A3
0 financial social social
1 game food sport
2 game sport game
Run Code Online (Sandbox Code Playgroud)
Expected:
financial food game social sport
0 1 0 0 2 0
1 0 1 1 0 1
2 0 0 2 0 1
Run Code Online (Sandbox Code Playgroud)
希望能得到帮助,谢谢!
使用pandas.get_dummies有sum:
df = pd.get_dummies(a, prefix_sep='', prefix='').sum(axis=1, level=0)
print (df)
financial game food social sport
0 1 0 0 2 0
1 0 1 1 0 1
2 0 2 0 0 1
Run Code Online (Sandbox Code Playgroud)
或者stack用SeriesGroupBy.value_counts和Series.unstack:
df = a.stack().groupby(level=0).value_counts().unstack(fill_value=0)
print (df)
financial food game social sport
0 1 0 0 2 0
1 0 1 1 0 1
2 0 0 2 0 1
Run Code Online (Sandbox Code Playgroud)