gpt4 book ai didi

python - pandas - 具有非数值的 pivot_table? (数据错误 : No numeric types to aggregate)

转载 作者:太空狗 更新时间:2023-10-29 18:16:06 33 4
gpt4 key购买 nike

我正在尝试对包含字符串作为结果的表进行数据透视。

import pandas as pd

df1 = pd.DataFrame({'index' : range(8),
'variable1' : ["A","A","B","B","A","B","B","A"],
'variable2' : ["a","b","a","b","a","b","a","b"],
'variable3' : ["x","x","x","y","y","y","x","y"],
'result': ["on","off","off","on","on","off","off","on"]})

df1.pivot_table(values='result',rows='index',cols=['variable1','variable2','variable3'])

但我得到:DataError: No numeric types to aggregate

当我将结果值更改为数字时,这会按预期工作:

df2 = pd.DataFrame({'index' : range(8),
'variable1' : ["A","A","B","B","A","B","B","A"],
'variable2' : ["a","b","a","b","a","b","a","b"],
'variable3' : ["x","x","x","y","y","y","x","y"],
'result': [1,0,0,1,1,0,0,1]})

df2.pivot_table(values='result',rows='index',cols=['variable1','variable2','variable3'])

我得到了我需要的:

variable1   A               B    
variable2 a b a b
variable3 x y x y x y
index
0 1 NaN NaN NaN NaN NaN
1 NaN NaN 0 NaN NaN NaN
2 NaN NaN NaN NaN 0 NaN
3 NaN NaN NaN NaN NaN 1
4 NaN 1 NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN 0
6 NaN NaN NaN NaN 0 NaN
7 NaN NaN NaN 1 NaN NaN

我知道我可以将字符串映射为数值然后反转操作,但也许有更优雅的解决方案?

最佳答案

我最初的回复是基于 Pandas 0.14.1,从那时起,pivot_table 函数发生了很多变化(行 --> 索引,列 --> 列...)

此外,我发布的原始 lambda 技巧似乎不再适用于 Pandas 0.18。您必须提供一个归约函数(即使它是最小值、最大值或平均值)。但即使那样似乎也不合适——因为我们没有减少数据集,只是对其进行转换……所以我更仔细地研究了 unstack……

import pandas as pd

df1 = pd.DataFrame({'index' : range(8),
'variable1' : ["A","A","B","B","A","B","B","A"],
'variable2' : ["a","b","a","b","a","b","a","b"],
'variable3' : ["x","x","x","y","y","y","x","y"],
'result': ["on","off","off","on","on","off","off","on"]})

# these are the columns to end up in the multi-index columns.
unstack_cols = ['variable1', 'variable2', 'variable3']

首先,使用索引+要堆叠的列在数据上设置索引,然后使用级别参数调用 unstack。

df1.set_index(['index'] + unstack_cols).unstack(level=unstack_cols)

结果数据框如下。

enter image description here

关于python - pandas - 具有非数值的 pivot_table? (数据错误 : No numeric types to aggregate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19279229/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com