我的数据帧就像
ID col1
1 Michael Owen
2 Stephen Curry
3 Messi, Lionel
4 James, LeBron
我试图颠倒那些被", "
分割的名字的顺序。你知道吗
我的密码是
df['col1'] = df.col1.str.split().apply(lambda x: ', '.join(x[::-1]))
但它会反转所有行,即使名称被" "
拆分。你知道吗
ID col1
1 Owen, Michael
2 Curry, Stephen
3 Lionel, Messi
4 LeBron, James
然后我试着
df.loc[df['col1'].str.contains(", ").split("col1")].apply(lambda x: ', '.join(x[::-1]))
它给了我一个错误
AttributeError: 'Series' object has no attribute 'split'
我怎样才能解决这个问题?你知道吗
使用^{} :
如果你想放下
','
这是因为
str.contains(", ")
返回一个布尔序列,而它没有方法split
。你知道吗无论如何,试试看
修复代码
np.where
相关问题 更多 >
编程相关推荐