【发布时间】:2018-03-28 09:26:03
【问题描述】:
我有一个我正在尝试旋转的 DataFrame。
df
col_1 col_2 col_3 col_4
John Method 4 White
Tom Method 29613 White
Harry Method 147 White
John Method 84 Blue
Tom Method 28 Blue
John Method 222085 Black
Tom Method 159459 Black
Harry Method 2204225 Black
John Method 600253 Green
Tom Method 3156210 Green
Harry Method 4343635 Green
Harry Method 4343635 Green
预期结果:
newDf
Black Blue Green White
Harry 2204225 8687270 147
John 222085 84 600253 4
Tom 159459 28 3156210 29613
我的代码:
newDf = pd.pivot_table(df, values='col_3', index=['col_1'], columns=['col_4'], aggfunc={'col_3' : 'sum'})
列类型如下:
df.dtypes
col_1 object
col_2 object
col_3 int64
col_4 object
dtype: object
错误:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
有人可以帮我吗?谢谢!
【问题讨论】:
-
您的错误无法重现,请告诉我们更多细节
-
您能否使用提供的示例 DF 重现该错误?你的 Pandas 版本是什么?