Shan-x
Shan-x

Reputation: 1176

Replace row while formating string

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?

Upvotes: 0

Views: 38

Answers (2)

Anton vBR
Anton vBR

Reputation: 18916

Normally when combining columns zip is very powerful. However with na-values that are to be dropped the solution would be more complicated. Something in the lines of:

df['Commentaires'] = [' - '.join(i) if np.nan not in i else np.nan 
                         for i in zip(df['Date'],df['Commentaires'])]

Upvotes: 0

jezrael
jezrael

Reputation: 862911

You can remove boolean mask:

df['Commentaires'] = df.Date + " - " + df.Commentaires

print (df)
    Date  Miles  Kilometres                   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

Upvotes: 1

Related Questions