[英]How to combine Multi-index and pivot to create a truth table in Pandas?
I have the following Pandas DataFrame, where column Z can have an unknown number of possible values eg A,B,C,D,E ... : 我有以下Pandas DataFrame,其中Z列可以具有未知数量的可能值,例如A,B,C,D,E ...:
X Y Z
-----
1 1 A
1 1 B
1 1 C
1 2 A
2 1 B
2 1 C
2 2 B
I would like to pivot on column Z and have multi-index on columns X,Y, to create a truth table like so: 我想对Z列进行透视,对X,Y列进行多索引,以创建一个真值表,如下所示:
X Y A B C
---------
1 1 T T T
1 2 T F F
2 1 F T T
2 2 F T F
How do I do this in pandas ? 如何在熊猫中做到这一点?
The following code does it (almost): 下面的代码(几乎)做到了:
pd.get_dummies(df,'Z').groupby(['X','Y']).max()
The following is closer to the exact output you asked for: 以下内容更接近您要求的确切输出:
(pd.get_dummies(df,'Z')
.groupby(['X','Y'])
.max()
.astype(bool)
.applymap(lambda s: str(s)[0])
.rename(columns=lambda v: v.replace('Z_','')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.