pivot_table无需聚合的数字类型

Dou*_*ger 8 python pandas

我想从与列下面的数据帧进行透视表sales,rep.数据透视表显示sales但没有rep.当我只尝试时rep,我得到了错误DataError: No numeric types to aggregate.如何解决这个问题,我看到数字字段sales和字段(字符串)rep

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
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 31

你可以使用set_indexunstack:

df = pd.DataFrame(data)
df.set_index(['year','country']).unstack('year')
Run Code Online (Sandbox Code Playgroud)

产量

          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
Run Code Online (Sandbox Code Playgroud)

或者,使用pivot_table具有aggfunc='first':

df.pivot_table(index='country', columns='year', values=['rep','sales'], aggfunc='first')
Run Code Online (Sandbox Code Playgroud)

产量

          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
Run Code Online (Sandbox Code Playgroud)

使用aggfunc='first', 通过获取找到的第一个值来聚合每个(country, year, rep)(country, year, sales)组.在您的情况下,似乎没有重复,因此第一个值与唯一值相同.

  • `aggfunc = 'first'` 太棒了。这正是对我有用的。 (8认同)