【发布时间】:2017-06-27 15:51:54
【问题描述】:
我在下面有一个数据框。
a = {'Id': ['ants', 'bees', 'cows', 'snakes', 'horses'], '2nd Attempts': [10, 12, 15, 14, 0],
'3rd Attempts': [10, 10, 9, 11, 10]}
a = pd.DataFrame(a)
print (a)
我希望能够将文本 ('-s') 添加到等于 4 个字符的任何内容。我没有成功尝试以下。当它产生错误时,ValueError:一个系列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
if a['Id'].str.len() == 3:
a['Id'] = a['Id'].str.replace('s', '-s')
else:
pass
【问题讨论】:
标签: python string pandas replace conditional-statements