[英]Update pandas dataframe and update the value if data exist
我有一个像这样的 csv 文件:
word, tag, counter
I, Subject, 1
Love, Verb, 3
Love, Adjective, 1
我想创建一个数据框,其中列是单词和标签列表,如下所示:
Word Subject Verb Adjective
I 1 0 0
Love 0 3 1
我如何设法用熊猫做到这一点?
您可以使用pivot
:
df = df.pivot(index='word', columns='tag', values='counter').fillna(0).astype(int)
print (df)
tag Adjective Subject Verb
word
I 0 1 0
Love 1 0 3
df = df.set_index(['word','tag'])['counter'].unstack(fill_value=0)
print (df)
tag Adjective Subject Verb
word
I 0 1 0
Love 1 0 3
但如果得到:
ValueError:索引包含重复条目,无法重塑
然后需要在pivot_table
通过一些aggfunc
进行pivot_table
:
print (df)
word tag counter
0 I Subject 1
1 Love Verb 3
2 Love Adjective 1 <-duplicates for Love and Adjective
3 Love Adjective 3 <-duplicates for Love and Adjective
df = df.pivot_table(index='word',
columns='tag',
values='counter',
aggfunc='mean',
fill_value=0)
print (df)
tag Adjective Subject Verb
word
I 0 1 0
Love 2 0 3
groupby
和unstack
另一个解决方案:
df = df.groupby(['word','tag'])['counter'].mean().unstack(fill_value=0)
print (df)
tag Adjective Subject Verb
word
I 0 1 0
Love 2 0 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.