遍历熊猫中的列

时间:2018-10-15 02:09:38

标签: python regex nlp

我想删除数据框中的所有数字。我做了这样的事情:

for row in df['msg'].iteritems():
    df['msg'][row] = re.sub(r"\d"," ",df['msg'][row])

但出现错误

<ipython-input-18-79f64a70b2a4> in <module>()
  4 import re
  5 for row in df['msg'].iteritems():
 ----> 6     df['msg'][row] = re.sub(r"\d"," ",df['msg'][row])
  7 print(df['msg'])

 C:\Users\Pratik\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
599         key = com._apply_if_callable(key, self)
600         try:
--> 601             result = self.index.get_value(self, key)
602 
603             if not is_scalar(result):

C:\Users\Pratik\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in 
get_value(self, series, key)
2426         try:
 2427             return self._engine.get_value(s, k,
->    2428                                           tz=getattr(series.dtype, 
 'tz', None))

1 个答案:

答案 0 :(得分:0)

pandas.Dataframe.replace可以理解 regex 。您可以尝试这种方式:

for col in df.columns:
    df[col] = df[col].astype(str).replace(r'\d', '', regex=True)