Skip to content Skip to sidebar Skip to footer

Merging Pandas DataFrames With The Same Column Name

I have a dataset, lets say: Column with duplicates value1 value2 1 5 0 1 0 9 And w

Solution 1:

IIUC, you can use groupby and then aggregate:

>>> df
   Column with duplicates  value1  value2
0                       1       5       0
1                       1       0       9

[2 rows x 3 columns]
>>> df.groupby("Column with duplicates", as_index=False).sum()
   Column with duplicates  value1  value2
0                       1       5       9

[1 rows x 3 columns]

On the OP's updated example:

>>> df
   trial       Time   1    2    3  4
0      1    '0-100'   0  100    0  0
1      1    '0-100'  32    0    0  0
2      1  '100-200'   0    0  100  0
3      2    '0-100'   0  100    0  0

[4 rows x 6 columns]
>>> df.groupby("trial", as_index=False).sum()
   trial   1    2    3  4
0      1  32  100  100  0
1      2   0  100    0  0

[2 rows x 5 columns]

Post a Comment for "Merging Pandas DataFrames With The Same Column Name"