5
我有一个数据框在下面。如果长度小于x,则替换字符串
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:一个Series的真值是不明确的。使用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