Most efficient way to melt dataframe with a ton of possible values pandas

大憨熊 提交于 2021-02-11 12:47:25

问题


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. 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?


回答1:


Check with crosstab

 out = pd.crosstab(df['Obs'], df['Category'])



回答2:


Isn't this crosstab:

pd.crosstab(df['Obs'], df['Category'])


来源:https://stackoverflow.com/questions/64419988/most-efficient-way-to-melt-dataframe-with-a-ton-of-possible-values-pandas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!