Home > Net >  How to merge two rows into a single one
How to merge two rows into a single one

Time:07-28

I have a df, looking like this and I would like to change it.

time lenght Weight
hh:mm:ss cm kg
12:05:26 150 80

into this :

time (hh:mm:ss) lenght (cm) Weight (kg)
12:05:26 150 80

CodePudding user response:

You should most certainly try to fix this issue during import of your data. Using for example header=[0, 1] in read_csv.

That said, you could use:

df2 = df.iloc[1:].set_axis([f'{a} ({b})' for a,b in zip(df.columns, df.iloc[0])], axis=1)

output:

  time (hh:mm:ss) lenght (cm) Weight (kg)
1        12:05:26         150          80

Note that that dtypes will probably be incorrect, which is why fixing the import is highly recommended.

  • Related