Pandas 透视数据框并根据它们是否存在将新列设置为 True/False
Pandas pivot dataframe and setting the new columns as True/False based on if they existed or not
如标题所述,我想旋转我的数据框(我认为它需要旋转?)
假设我有一个如下所示的 df:
df = pd.DataFrame({'ID' : [0, 0, 1, 1, 1],
'REV' : [0, 0, 1, 1, 1],
'GROUP' : [1, 2, 1, 2, 3]})
+----+-----+-------+
| ID | REV | GROUP |
+----+-----+-------+
| 0 | 0 | 1 |
| 0 | 0 | 2 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
+----+-----+-------+
我想做一些旋转,所以 table 的结果看起来像
+----+-----+------+------+-------+
| ID | REV | 1 | 2 | 3 |
+----+-----+------+------+-------+
| 0 | 0 | True | True | False |
| 1 | 1 | True | True | True |
+----+-----+------+------+-------+
现在 GROUP 列中的值变成了自己的列。根据原始 df 是否有该组,这些列中每一列的值都是 T/F。
有什么建议吗?这看起来像是一个枢轴的东西,但在使用枢轴时我是一个大菜鸟
我会使用 get_dummies
然后 groupby
和 any
:
pd.get_dummies(df.set_index(["ID", "REV"]).GROUP).groupby(level=[0,1]).any()
1 2 3
ID REV
0 0 True True False
1 1 True True True
如果您希望 ID
和 REV
列作为列而不是索引,您可以添加 reset_index
:
pd.get_dummies(df.set_index(["ID", "REV"]).GROUP).groupby(level=[0,1]).any().reset_index()
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True
试试 crosstab
out = pd.crosstab([df.ID,df.REV],df.GROUP).ne(0).reset_index().rename_axis(None,axis=1)
out
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True
您可以创建一个与 GROUP
相同的虚拟列,然后将该虚拟列用作 pivot_table()
中的值。
df['GROUP_'] = df['GROUP']
df_ = ~ pd.pivot_table(df, index=['ID', 'REV'], columns='GROUP', values='GROUP_').isna()
print(df_)
GROUP 1 2 3
ID REV
0 0 True True False
1 1 True True True
print(df_.reset_index().rename_axis(None,axis=1))
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True
如标题所述,我想旋转我的数据框(我认为它需要旋转?)
假设我有一个如下所示的 df:
df = pd.DataFrame({'ID' : [0, 0, 1, 1, 1],
'REV' : [0, 0, 1, 1, 1],
'GROUP' : [1, 2, 1, 2, 3]})
+----+-----+-------+
| ID | REV | GROUP |
+----+-----+-------+
| 0 | 0 | 1 |
| 0 | 0 | 2 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
+----+-----+-------+
我想做一些旋转,所以 table 的结果看起来像
+----+-----+------+------+-------+
| ID | REV | 1 | 2 | 3 |
+----+-----+------+------+-------+
| 0 | 0 | True | True | False |
| 1 | 1 | True | True | True |
+----+-----+------+------+-------+
现在 GROUP 列中的值变成了自己的列。根据原始 df 是否有该组,这些列中每一列的值都是 T/F。
有什么建议吗?这看起来像是一个枢轴的东西,但在使用枢轴时我是一个大菜鸟
我会使用 get_dummies
然后 groupby
和 any
:
pd.get_dummies(df.set_index(["ID", "REV"]).GROUP).groupby(level=[0,1]).any()
1 2 3
ID REV
0 0 True True False
1 1 True True True
如果您希望 ID
和 REV
列作为列而不是索引,您可以添加 reset_index
:
pd.get_dummies(df.set_index(["ID", "REV"]).GROUP).groupby(level=[0,1]).any().reset_index()
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True
试试 crosstab
out = pd.crosstab([df.ID,df.REV],df.GROUP).ne(0).reset_index().rename_axis(None,axis=1)
out
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True
您可以创建一个与 GROUP
相同的虚拟列,然后将该虚拟列用作 pivot_table()
中的值。
df['GROUP_'] = df['GROUP']
df_ = ~ pd.pivot_table(df, index=['ID', 'REV'], columns='GROUP', values='GROUP_').isna()
print(df_)
GROUP 1 2 3
ID REV
0 0 True True False
1 1 True True True
print(df_.reset_index().rename_axis(None,axis=1))
ID REV 1 2 3
0 0 0 True True False
1 1 1 True True True