在pandas Series / DataFrame上执行多个字符串操作

时间:2018-05-23 17:28:01

标签: python pandas

我想对特定系列执行几项操作。有没有办法在不连续写.str的情况下将它们链接起来?即如果我的系列被称为s并且我想做

s.str.replace("hi", "bye").str.strip().str.lower()

这是正确的做事方式吗?看起来相对于R的冗长,所以我想也许有更好的语法。

1 个答案:

答案 0 :(得分:2)

理解

是(sorta)。使用理解

[x.replace('hi', 'bye').strip().lower() for x in s]

再把它包装成一个系列。

pd.Series([x.replace('hi', 'bye').strip().lower() for x in s], s.index)

map

s.map(lambda x: x.replace('hi', 'bye').strip().lower())