[英]Most efficient way to melt dataframe with a ton of possible values pandas
I have a long format dataframe like the following:我有一个长格式数据框,如下所示:
In reality, my dataframe is about 10k rows and there are about 400 possible values of category.实际上,我的数据框大约有 10k 行,大约有 400 个可能的类别值。 I'd like to melt my dataframe into a wide format like the following:
我想将我的数据框融合为如下所示的宽格式:
I can think of a couple ways to do this, but what would be an efficient way to do this transformation?我可以想到几种方法来做到这一点,但是进行这种转换的有效方法是什么?
这不是交叉表吗:
pd.crosstab(df['Obs'], df['Category'])
用crosstab
检查
out = pd.crosstab(df['Obs'], df['Category'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.