Wesley Bowman
Wesley Bowman

Reputation: 1396

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 what I want

Column with duplicates        value1       value2
        1                        5            9

I cannot figure out how to get this to work. The closest I got was using merge, but that left me with different suffixes.

Any ideas?

My real data looks like:

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

I want to keep the trials separate, and just merge the Times

Upvotes: 0

Views: 1511

Answers (1)

DSM
DSM

Reputation: 353099

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]

Upvotes: 2

Related Questions