尝试使用crosstab
pd.crosstab(df['unique id'],df['keyword'])
Use groupby
apply
with Counter
, untack
and fillna
:
from collections import Counter
df.groupby('unique id')['keyword'].apply(Counter).to_frame().unstack(1).fillna(0)
keyword
auto fish mobile
unique id
1a 2.0 1.0 1.0
2a 1.0 2.0 1.0
3a 0.0 1.0 0.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.