我正在尝试使用从CSV文件导出的表格来显示Google图表中的大量数据。这是我的表格的样子(表格在pandas中为df ):
name | date | location | counta | countb
Joe | 2013 | USA | 13 | 15
Jack | 2015 | Spain | 21 | 5
Carl | 2013 | Russia | 2 | 1
Joe | 2015 | USA | 42 | 9
Carl | 2015 | Russia | 3 | 23
我做的第一件事是做一个支点
df = df.pivot_table(value = ['counta', countb'], index = ['date', 'location'], columns='name'
退回此:
.....|..... | Joe | Joe | Jack | Jack | Carl | Carl
date |location| counta | countb | counta | countb | counta | countb
2013 | USA | 13 | 15 | 0 |0 | 0 | 0
2013 | Russia | 0 | 0 | 0 |0 | 2 | 1
2013 | Spain | 0 | 0 | 0 |0 | 0 | 0
2014 | USA | 0 | 0 | 0 |0 | 0 | 0
2014 | Russia | 0 | 0 | 0 |0 | 0 | 0
2014 | Spain | 0 | 0 | 0 |0 | 0 | 0
2015 | USA | 42 | 9 | 0 |0 | 0 | 0
2015 | Russia | 0 | 0 | 0 |0 | 3 | 23
2015 | Spain | 0 | 0 | 21 |5 | 0 | 0
将新df
保存到csv文件:
df.to_csv("test.csv")
如何显示谷歌图表:
问题:是否可以连接(name,counta)和concat(name,countb)?
答案 0 :(得分:1)
让我们用以下内容展平专栏:
#You might need to swaplevel first
df1 = df1.swaplevel(0,1,axis=1)
df1.columns = df1.columns.map('_'.join)