Home > Software design >  How to combine rows with same id number using pandas in python?
How to combine rows with same id number using pandas in python?

Time:07-07

I have a big csv file and I would like to combine rows with the same id#. For instance, this is what my csv shows right now. enter image description here

and I would like it to be like this:

enter image description here

how can I do this using pandas?

CodePudding user response:

Try this:

df = df.groupby('id').agg({'name':'last', 
                           'type':'last', 
                           'date':'last' }).reset_index()

this way you can have customized function in handling each columns. (By changing the function from 'last' to your function)

CodePudding user response:

You can read the csv with pd.read_csv() function and then use the GroupBy.last() function to aggregate rows with the same id.

something like:

df = pd.read_csv('file_name.csv')
df1 = df.groupby('id').last()

you should also decide an aggregation function instead of using "the last" row value.

  • Related