0

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?

2
  • 1
    How working df['Commentaires'] = df.Date + " - " + df.Commentaires ? Commented Oct 19, 2018 at 10:26
  • 1
    Yes, why wouldnt that work? df.Date + " - " + df.Commentaires (will transform the N/A in N/A) Commented Oct 19, 2018 at 10:26

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Keep It Simple, Stupid: I didn't think it could be so easy.
@Shan-x - No problem, its happen ;)
0

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'])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.