[英]How to pivot pandas DataFrame column to create binary “value table”?
I have the following pandas dataframe: 我有以下熊猫数据框:
import pandas as pd
df = pd.read_csv("filename.csv")
df
A B C D E
0 a 0.469112 -0.282863 -1.509059 cat
1 c -1.135632 1.212112 -0.173215 dog
2 e 0.119209 -1.044236 -0.861849 dog
3 f -2.104569 -0.494929 1.071804 bird
4 g -2.224569 -0.724929 2.234213 elephant
...
I would like to create more columns based on the identity of categorical values in column E
such that the dataframe looks like this: 我想基于
column E
的分类值的标识创建更多列,以使数据框如下所示:
df
A B C D cat dog bird elephant ....
0 a 0.469112 -0.282863 -1.509059 -1 0 0 0
1 c -1.135632 1.212112 -0.173215 0 -1 0 0
2 e 0.119209 -1.044236 -0.861849 0 -1 0 0
3 f -2.104569 -0.494929 1.071804 0 0 -1 0
4 g -2.224569 -0.724929 2.234213 0 0 0 0
...
That is, I pivot the values for column E
to be a binary matrix based on the values of E
, giving 1
if the value exists, and 0
for all others where it doesn't (here, I would like it to be -1
or a "negative binary matrix")? 也就是说,我转动的值列
E
是基于对值的二进制矩阵E
,给1
,如果该值存在,并且0
的地方没有其他所有(在这里,我想它是-1
或“负二进制矩阵”)?
I'm not sure which function in pandas best does this: maybe pandas.DataFrame.unstack()
? 我不确定pandas中哪个函数最能做到这一点:也许
pandas.DataFrame.unstack()
吗?
Any insight appreciated! 任何见解表示赞赏!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.