'How to combine the attributes based on the two columns

I have a pandas dataframe imported from the web as below.

Unfortunately, the values for the size of the bag are in different rows.

Name Bag Length Width Height
James A 15.32
James A 27.33
James B 20.69
James B 15.87
Ausines A 17.88
Ausines A 18.94
Ausines A 14.56
Ausines B 16.82

I would like to express the size attributes in a single line based on the 'name' and the 'bag' column as follows :

Name Bag Length Width Height
James A 15.32 27.33
James B 15.87 20.69
Ausines A 17.88 18.94 14.56
Ausines B 16.82

How can I solve this problem? I'd appreciate it if you could help me.



Solution 1:[1]

Use groupby_first:

out = df.groupby(['Name', 'Bag'], sort=False, as_index=False).first()
print(out)

# Output
      Name Bag  Length  Width  Height
0    James   A   15.32  27.33     NaN
1    James   B     NaN  15.87   20.69
2  Ausines   A   17.88  18.94   14.56
3  Ausines   B     NaN  16.82     NaN

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien