1

Is there any way to concatenate multiple rows into one as the exemple below:

   Name   Date_1     Date_2     Date_3
0  name1     11        NaT       NaT
1  name1     NaT       12        NaT
2  name1     NaT       NaT       43
3  name2     12        NaN       NaT
4  name2     NaT       Text      NaT

So it could be NaT or NaN.

I'm looking to it:

 Name   Date_1     Date_2     Date_3
 name1     11        12        43
 name2     12        Text       NaT

Thanks in advance

Alex Germain
  • 411
  • 4
  • 16

1 Answers1

3

You haven't stated how you want to handle duplicates but to get the min/max you can use the following:

import pandas as pd

df = pd.DataFrame(
    [
        {"name": "name1", "date_1": 11},
        {"name": "name1", "date_2": 12},
        {"name": "name1", "date_3": 43},
        {"name": "name2", "date_1": 12},
    ],
    columns=["name", "date_1", "date_2", "date_3"],
)

df.groupby('name').max().reset_index()

which gives:

    name  date_1  date_2  date_3
0  name1    11.0    12.0    43.0
1  name2    12.0     NaN     NaN
kfoley
  • 320
  • 1
  • 9
  • Thanks for your answer, I have only one value between date_1, date_2... but I have both string and number. max() is quite working with date/number type but not with text (my fault I didn't said that there are some string) – Alex Germain Jan 16 '19 at 13:37
  • 1
    You can use `first()` to get the first value in the column for that group. – kfoley Jan 16 '19 at 13:46
  • However, you'll likely want to covert all values to numeric. See https://stackoverflow.com/questions/15891038/change-data-type-of-columns-in-pandas for info on that – kfoley Jan 16 '19 at 13:55