如果我的数据框类似于以下内容
Year Month Date Country Counts
2010 01 01 USA 10
2010 01 01 Canada 20
2010 01 01 Russia 13
2010 01 01 Brazil 14
2010 01 01 China 21
2010 01 02 USA 11
2010 01 02 Canada 21
2010 01 02 Russia 14
2010 01 02 Brazil 15
2010 01 02 China 22
如何将其转换为以下内容?
Year Month Date Country Counts USA Canada Russia Brazil China
2010 01 01 USA 10 10 0 0 0 0
2010 01 01 Canada 20 0 20 0 0 0
2010 01 01 Russia 13 0 0 13 0 0
2010 01 01 Brazil 14 0 0 0 14 0
2010 01 01 China 21 0 0 0 0 21
2010 01 02 USA 10 11 0 0 0 0
2010 01 02 Canada 20 0 21 0 0 0
2010 01 02 Russia 13 0 0 14 0 0
2010 01 02 Brazil 14 0 0 0 15 0
2010 01 02 China 21 0 0 0 0 22
答案 0 :(得分:3)
使用DataFrame.set_index
和Series.unstack
进行透视,按DataFrame.reindex
更改列顺序,并按DataFrame.join
添加到原始列:
df1 = (df.join(df.set_index('Country', append=True)['Counts']
.unstack(fill_value=0)
.reindex(df['Country'].unique(), axis=1)))
print (df1)
Year Month Date Country Counts USA Canada Russia Brazil China
0 2010 1 1 USA 10 10 0 0 0 0
1 2010 1 1 Canada 20 0 20 0 0 0
2 2010 1 1 Russia 13 0 0 13 0 0
3 2010 1 1 Brazil 14 0 0 0 14 0
4 2010 1 1 China 21 0 0 0 0 21
5 2010 1 2 USA 11 11 0 0 0 0
6 2010 1 2 Canada 21 0 21 0 0 0
7 2010 1 2 Russia 14 0 0 14 0 0
8 2010 1 2 Brazil 15 0 0 0 15 0
9 2010 1 2 China 22 0 0 0 0 22