我有一个数据帧,有点像这样:
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”列上的内容(如果无法直接进行重命名,我可以稍后重命名它们,只是想说明我的观点
现在我已经说过了,我想补充一点,我已经尝试了不同的方法,似乎用unstack
或pivot
是一条路要走,但我没有成功地使它发挥作用
任何帮助都将不胜感激
此解决方案假定数据帧由ID列索引
如果这不是我们想要的,请告诉我
相关问题 更多 >
编程相关推荐