我有一个这样的熊猫数据帧:
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
我想连接列Date
,如果不Nan
Commentaires
Commentaires
:
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
以下代码片段运行良好:
df.loc[(pd.notnull(df.Commentaires), 'Commentaires')] = df.Date + " - " + df.Commentaires
但它不是很蟒蛇。我宁愿这样做:
df.loc[(pd.notnull(df.Commentaires), 'Commentaires')] = "{Date} - {Commentaires}".format(df)
但是我有一个KeyError: 'Date'
.
其他解决方案,其他问题:
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/04n1 08/04n2 09/04n3 ...
1 08/04 22 35 NaN
2 09/04 19 31 0 07/04n1 08/04n2 09/04n3 ...
3 10/04 20 32 0 07/04n1 08/04n2 09/04n3 ...
4 11/04 7 11 0 07/04n1 08/04n2 09/04n3 ...
我怎样才能以最pythonic的方式获得我想要的结果?
您可以删除布尔掩码:
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
通常,当组合列时,zip 非常强大。但是,对于要删除的 na 值,解决方案将更加复杂。以下几行
:df['Commentaires'] = [' - '.join(i) if np.nan not in i else np.nan
for i in zip(df['Date'],df['Commentaires'])]