为此,我认为我不会尝试任何太可爱的事情:只需弄清楚哪些行需要移动东西,然后移动它们。从像这样的框架开始
>>> df
Website Notes Other
0 http://stackoverflow.com home away from home a
1 http://mapleleafs.nhl.com/ 1967 b
2 see over http://www.example.com/not_so_long c
3 http://www.colts.com/ the Luck of the Hoosiers d
我会做类似的事情
>>> to_shift_over = df.Website.str.lower().str.contains("see over")
>>> df.loc[to_shift_over, "Website"] = df["Notes"]
>>> df.loc[to_shift_over, "Notes"] = ""
生产
>>> df
Website Notes Other
0 http://stackoverflow.com home away from home a
1 http://mapleleafs.nhl.com/ 1967 b
2 http://www.example.com/not_so_long c
3 http://www.colts.com/ the Luck of the Hoosiers d
使用str
on aSeries
是对它们执行矢量操作的便捷方法:
>>> df["Website"].str
<pandas.core.strings.StringMethods object at 0xa9dcfac>
>>> df["Website"].str.lower()
0 http://stackoverflow.com
1 http://mapleleafs.nhl.com/
2 see over
3 http://www.colts.com/
Name: Website, dtype: object
>>> df["Website"].str.lower().str.contains("see over")
0 False
1 False
2 True
3 False
Name: Website, dtype: bool
然后我们可以使用该布尔数组来索引df
using .loc
:
>>> df.loc[to_shift_over]
Website Notes Other
2 see over http://www.example.com/not_so_long c
>>> df.loc[to_shift_over, "Website"]
2 see over
Name: Website, dtype: object