I have a pandas dataframe like this:
Date Miles Kilomètres Commentaires
0 07/04 17 27 string1
1 08/04 22 35 NaN
2 09/04 19 31 string2
3 10/04 20 32 string2
4 11/04 7 11 Another random string
I want to concatenate columns Date and Commentaires if Commentaires is not Nan:
Date Miles Kilomètres Commentaires
0 07/04 17 27 07/04 - string1
1 08/04 22 35 NaN
2 09/04 19 31 09/04 - string2
3 10/04 20 32 10/04 - string2
4 11/04 7 11 11/04 - Another random string
The following snippet is working well:
df.loc[(pd.notnull(df.Commentaires), 'Commentaires')] = df.Date + " - " + df.Commentaires
But it's not very pythonic. I'd rather do that:
df.loc[(pd.notnull(df.Commentaires), 'Commentaires')] = "{Date} - {Commentaires}".format(df)
But then I have a KeyError: 'Date'.
Other solution, other problem:
df.loc[(pd.notnull(df.Commentaires), 'Commentaires')] = "{} - {}".format(df.Date, df.Commentaires)
print(df.head())
Date Miles Kilomètres Commentaires
0 07/04 17 27 0 07/04\n1 08/04\n2 09/04\n3 ...
1 08/04 22 35 NaN
2 09/04 19 31 0 07/04\n1 08/04\n2 09/04\n3 ...
3 10/04 20 32 0 07/04\n1 08/04\n2 09/04\n3 ...
4 11/04 7 11 0 07/04\n1 08/04\n2 09/04\n3 ...
How can I obtain the result I want in the most pythonic way?
df['Commentaires'] = df.Date + " - " + df.Commentaires?df.Date + " - " + df.Commentaires(will transform the N/A in N/A)