Nairda123
Nairda123

Reputation: 313

Pandas - How to combine duplicate items into one with several columns

I have the below DataFrame

dataframe

As you can see, ItemNo 1 is duplicated three times, and each column has a value corresponding to it.

I am looking for a method to check against all columns, and if they match then put Price, Sales, and Stock as one entry, not three.

Any help will be greatly appreciated.

Upvotes: 0

Views: 908

Answers (1)

Pawan Jain
Pawan Jain

Reputation: 825

Simply remove all the NaN instances and redefine the column names

df = df1.apply(lambda x: pd.Series(x.dropna().values), axis=1)
df.columns = ['ItemNo','Category','SIZE','Model','Customer','Week Date','<New col name>']

For converging to one row, you can use groupby like this

df.groupby('ItemNo', as_index=False).first()

Upvotes: 1

Related Questions