我想從以下數據框中創建一個數據透視表,其列sales
,rep
。數據透視表顯示sales
,但不顯示rep
。當我只用rep
嘗試時,我得到了錯誤DataError: No numeric types to aggregate
。如何解決這一問題,使得我看到這兩個數字字段sales
和場(串)rep
pivot_table沒有要彙總的數字類型
data = {'year': ['2016', '2016', '2015', '2014', '2013'],
'country':['uk', 'usa', 'fr','fr','uk'],
'sales': [10, 21, 20, 10,12],
'rep': ['john', 'john', 'claire', 'kyle','kyle']
}
print pd.DataFrame(data).pivot_table(index='country', columns='year', values=['rep','sales'])
sales
year 2013 2014 2015 2016
country
fr NaN 10 20 NaN
uk 12 NaN NaN 10
usa NaN NaN NaN 21
print pd.DataFrame(data).pivot_table(index='country', columns='year', values=['rep'])
DataError: No numeric types to aggregate
這取決於你正在努力去做。默認的agg函數是'mean',你不能考慮銷售代表的意思。請更改agg函數或傳遞另一列的值。如果你只是想使用樞軸,使用樞軸而不是pivot_table。 – ayhan