如何在 pandas 中旋转分类变量? [英] How to pivot categorical variable in pandas?
本文介绍了如何在 pandas 中旋转分类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何透视这些数据?
date - low - up
d1 - a - A
d1 - b - B
d2 - c - C
进入这个
low a - b - c
date
d1 A - B - NaN
d2 NaN-NaN- C
我正在使用pivot_table/pivot,但它总是要求输入数字.如何旋转分类变量?谢谢
I'm using pivot_table/pivot, but it always asking for numerical. How to pivot categorical variables? Thanks
推荐答案
使用 pandas 版本 0.15.2
我没有看到任何问题:
With pandas version 0.15.2
I don't see any problems:
import pandas as pd
# I copied your example data and then read it in with
df = pd.read_clipboard(sep=r'\s+-\s+')
df
Out[3]:
date low up
0 d1 a A
1 d1 b B
2 d2 c C
df.pivot(index='date', columns='low', values='up')
Out[5]:
low a b c
date
d1 A B NaN
d2 NaN NaN C
如果此代码对您不起作用,您应该使用 pd.version.version
检查您的 Pandas 版本,如果您落后几个版本,请考虑升级.
If this code doesn't work for you you should check your pandas version with pd.version.version
and consider upgrading if you're a few releases behind.
这篇关于如何在 pandas 中旋转分类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文