0

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?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 1
    Does this answer your question? [pandas group by and find first non null value for all columns](https://stackoverflow.com/questions/59048308/pandas-group-by-and-find-first-non-null-value-for-all-columns) – Chris Jul 06 '22 at 17:07

2 Answers2

0

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)

ytung-dev
  • 872
  • 1
  • 2
  • 12
0

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.

Matteo Buffagni
  • 312
  • 2
  • 10