替换数据以' ...'开头。与np.NaN

时间:2017-07-14 20:44:45

标签: python regex pandas dataframe

我有一个DataFrame,字符串数据以' ...'开头。如何用np.NaN?

替换DataFrame中的字符串值

我使用了以下内容:df.replace('...', np.NaN, inplace=True) 。当然,并没有用' ...'替换所有数据。

我打算将regExp用作df.replace('^\.',np.NaN, inplace=True) 但它没有用。

有人可以帮助我将正则表达式传递到df.replace吗?

1 个答案:

答案 0 :(得分:2)

使用df.replace,指定regex=True

In [447]: df = pd.DataFrame({'Col1' : ['foo', '...', 'bar', '...test', '...']})

In [448]: df.replace(r'^\.\.\..*', np.nan, regex=True)
Out[448]: 
  Col1
0  foo
1  NaN
2  bar
3  NaN
4  NaN