在Pandas中取消堆栈(pivot?)数据帧

2024-10-04 15:26:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,有点像这样:

   ID | Relationship | First Name | Last Name |     DOB     |     Address   |    Phone
0 | 2 |     Self     |   Vegeta   |  Saiyan   |  01/01/1949 | Saiyan Planet | 123-456-7891
1 | 2 |     Spouse   |   Bulma    |  Saiyan   |  04/20/1969 | Saiyan Planet | 123-456-7891
2 | 3 |     Self     |   Krilin   |  Human    |  08/21/1992 | Planet Earth  | 789-456-4321
3 | 4 |     Self     |   Goku     |  Kakarot  |  05/04/1975 | Planet Earth  | 321-654-9870
4 | 4 |     Child    |   Gohan    |  Kakarot  |  04/02/2001 | Planet Earth  | 321-654-9870
5 | 5 |     Self     |   Freezer  |  Fridge   |  09/15/1955 |  Deep Space   | 456-788-9568

我希望将具有相同ID的行附加到具有该ID的第一行的右侧

例如:

   ID | Relationship | First Name | Last Name |     DOB     |     Address   |    Phone     |  Spouse_First Name |  Spouse_Last Name  |  Spouse_DOB  |  Child_First Name  |  Child_Last Name  |   Child_DOB   |
0 | 2 |     Self     |   Vegeta   |  Saiyan   |  01/01/1949 | Saiyan Planet | 123-456-7891 |      Bulma         |        Saiyan      |   04/20/1969 |                    |                   |
1 | 3 |     Self     |   Krilin   |  Human    |  08/21/1992 | Planet Earth  | 789-456-4321 |                    |                    |              |                    |                   |
2 | 4 |     Self     |   Goku     |  Kakarot  |  05/04/1975 | Planet Earth  | 321-654-9870 |                    |                    |              |        Gohan       |      Kakarot      |   04/02/2001  | 
3 | 5 |     Self     |   Freezer  |  Fridge   |  09/15/1955 |  Deep Space   | 456-788-9568 |                    |                    |              |                    |                   |

我的真实场景dataframe有更多的列,但是当两行共享相同的ID时,它们都具有相同的信息,因此不需要复制其他行中的列。我只需要将我选择的列添加到右边,在本例中是First Name, Last Name, DOB,新列标签的标识符取决于“Relationship”列上的内容(如果无法直接进行重命名,我可以稍后重命名它们,只是想说明我的观点

现在我已经说过了,我想补充一点,我已经尝试了不同的方法,似乎用unstackpivot是一条路要走,但我没有成功地使它发挥作用

任何帮助都将不胜感激


Tags: nameselfidchildaddressphonefirstlast
1条回答
网友
1楼 · 发布于 2024-10-04 15:26:39

此解决方案假定数据帧由ID列索引

not_self = (
    df.query("Relationship != 'Self'")
    .pivot(columns='Relationship')
    .swaplevel(axis=1)
    .reindex(
        pd.MultiIndex.from_product(
            (
                set(df['Relationship'].unique()) - {'Self'}, 
                df.columns.to_series().drop('Relationship')
            )
        ),
        axis=1
    )
)
not_self.columns = [' '.join((a, b)) for a, b in not_self.columns]
result = df.query("Relationship == 'Self'").join(not_self)

如果这不是我们想要的,请告诉我

相关问题 更多 >

    热门问题