Python 仅将文本附加到Dataframe的非空值
我有一个df,看起来像这样:Python 仅将文本附加到Dataframe的非空值,python,pandas,Python,Pandas,我有一个df,看起来像这样: | id | qty | item | +-----+------+------+ | 001 | 700 | CB04 | | 002 | 500 | | | 003 | 1500 | AB01 | 我想将文本框附加到df['item']中,其中项目不为空,因此新的df将如下所示: | id | qty | item | +-----+------+----------+ | 001 | 700 | CB04 box | | 00
| id | qty | item |
+-----+------+------+
| 001 | 700 | CB04 |
| 002 | 500 | |
| 003 | 1500 | AB01 |
我想将文本框
附加到df['item']
中,其中项目不为空,因此新的df将如下所示:
| id | qty | item |
+-----+------+----------+
| 001 | 700 | CB04 box |
| 002 | 500 | |
| 003 | 1500 | AB01 box |
对于我来说,不带复选框的工作解决方案
NaN
s:
df['item'] += ' box'
print (df)
id qty item
0 1 700 CB04 box
1 2 500 NaN
2 3 1500 AB01 box
带有checkNaN
s的解决方案:
df['item'] += ' box'
print (df)
id qty item
0 1 700 CB04 box
1 2 500 NaN
2 3 1500 AB01 box
与loc一起使用
df.loc[df['item'].notna(), 'item'] += ' box'
#for oldier pandas versions
#df.loc[df['item'].notnull(), 'item'] += ' box'
print (df)
id qty item
0 1 700 CB04 box
1 2 500 NaN
2 3 1500 AB01 box
或: