2

I have a dataframe that looks like this:

productID units sold units in inventory
101 32 NaN
102 45 NaN
103 15 NaN
104 27 NaN
101 NaN 18
102 NaN 12
103 NaN 30
104 NaN 23

As you can see, the first column contains duplicates, where each instance has data in one 'data' column, but not the other 'data' column.

Is there a way to merge the rows, so the dataframe looks like this?

productID units sold units in inventory
101 32 18
102 45 12
103 15 30
104 27 23
jub
  • 377
  • 1
  • 2
  • 14
  • [`Groupby.first`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.first.html) -> `df.groupby('productID', as_index=False).first()` – Henry Ecker Aug 29 '21 at 02:03

1 Answers1

1

Try groupby.first:

>>> df.groupby('productID', as_index=False).first()
   productID  units sold  units in inventory
0        101        32.0                18.0
1        102        45.0                12.0
2        103        15.0                30.0
3        104        27.0                23.0
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114