这是我关于stackoverflow的第一个问题!请耐心等待:)
我想在DataFrame的某些行中添加文本。原始数据框如下所示:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'Name and rooms' : ['Excalibur: 1 room','John: 2 rooms','1 room','Lucas: 5 rooms','4 rooms','Jeremy: 1 room']})
In [3]: df
Out[3]:
Name and rooms
0 Excalibur: 1 room
1 John: 2 rooms
2 1 room
3 Lucas: 5 rooms
4 4 rooms
5 Jeremy: 1 room
如您所见,有些行缺少名称。我想在没有名称的行中添加一些固定的字符串(比如“Whatever:”,无论是什么字符串)(在本例中,第2行和第4行)。最终的数据集如下所示:
In [11]: df
Out[11]:
Name and rooms
0 Excalibur: 1 room
1 John: 2 rooms
2 Whatever: 1 room
3 Lucas: 5 rooms
4 Whatever: 4 rooms
5 Jeremy: 1 room
我是pandas / python的新手,所以任何帮助都会非常感激。
谢谢!
答案 0 :(得分:1)
使用向量化str
方法contains
创建布尔掩码并使用否定运算符~
,将其传递给loc
并将字符串前置为当前值:
In [83]:
df.loc[~df['Name and rooms'].str.contains(':'),'Name and rooms'] = 'Whatever: ' + df['Name and rooms']
df
Out[83]:
Name and rooms
0 Excalibur: 1 room
1 John: 2 rooms
2 Whatever: 1 room
3 Lucas: 5 rooms
4 Whatever: 4 rooms
5 Jeremy: 1 room