我有一个数据框:
Year Type Total
0 2005 Journal Article 635
1 2005 Book 173
2 2005 Book Chapter 27
3 2005 Consultancy Report 1
4 2005 Working Paper 329
.....
然后我转动它以使其成为合适的格式(借助数据透视表的格式,这是出于说明的目的):
dfA = pd.pivot_table(dfPub, index=['Year'], columns=['Type'], fill_value=0)
Total
Type Book Book Chapter Book edited Conference Proceeding_Abstract Consultancy Report Journal Article Manual / Guide Manual/Guide Monograph Other Policy briefing_report Scholarly edition Systematic review Technical Report Technical Standard Thesis Working Paper
Year
2005 173 27 0 174 1 635 0 0 0 7 0 0 0 81 0 1 329
2006 272 53 0 276 0 1078 0 0 0 14 2 0 0 91 0 11 532
2007 321 114 0 295 1 1588 0 0 0 5 2 0 0 139 0 10 548
2008 397 194 2 502 1 2237 0 0 4 19 4 0 0 142 0 10 580
2009 402 298 4 927 5 2876 0 0
在我的传奇中每''Total','
之前放置'Type'
我做错了什么?有没有办法删除它?谢谢
我尝试过编辑列名,但它不起作用:
dfA.rename(columns={"('Total', 'Book')":"Book", "('Total', 'Book Chapter')":"Book Chapter"})
list(dfA)
[('Total', 'Book'),
('Total', 'Book Chapter'),
('Total', 'Book edited'),
('Total', 'Conference Proceeding_Abstract'),
('Total', 'Consultancy Report'),
('Total', 'Journal Article'),
('Total', 'Manual / Guide'),
('Total', 'Manual/Guide'),
('Total', 'Monograph'),
('Total', 'Other'),
('Total', 'Policy briefing_report'),
('Total', 'Scholarly edition'),
('Total', 'Systematic review'),
('Total', 'Technical Report'),
('Total', 'Technical Standard'),
('Total', 'Thesis'),
('Total', 'Working Paper')]
答案 0 :(得分:1)
您为剧情提供的表格有两个列标题。作为整个表的第一列标题和作为第二列的正确列标题的年份,因此它标记了每个字段的总计。尝试重命名列标题,或删除列标题并重命名。