我想从以下带有列sales,的数据框中创建数据透视表rep。数据透视表显示,sales但不显示rep。当我只用尝试时rep,我得到了错误DataError: No numeric types to aggregate。如何解决此问题,使我同时看到数字字段sales和字段(字符串)rep
sales
rep
DataError: No numeric types to aggregate
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
您可以使用set_index和unstack:
set_index
unstack
df = pd.DataFrame(data) df.set_index(['year','country']).unstack('year')
产量
rep sales year 2013 2014 2015 2016 2013 2014 2015 2016 country fr None kyle claire None NaN 10.0 20.0 NaN uk kyle None None john 12.0 NaN NaN 10.0 usa None None None john NaN NaN NaN 21.0
或者,pivot_table与配合使用aggfunc='first':
pivot_table
aggfunc='first'
df.pivot_table(index='country', columns='year', values=['rep','sales'], aggfunc='first')
rep sales year 2013 2014 2015 2016 2013 2014 2015 2016 country fr None kyle claire None None 10 20 None uk kyle None None john 12 None None 10 usa None None None john None None None 21
使用aggfunc='first', 通过获取找到的第一个值对每个(country, year, rep)或(country, year, sales)组进行聚合。在您的情况下,似乎没有重复项,因此第一个值与唯一的值相同。
(country, year, rep)
(country, year, sales)