简体   繁体   中英

iterating over columns in pandas

I want to remove all the digits in my dataframe. I did something like this:

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

but got an error

<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))

pandas.Dataframe.replace can understand regex . You can try this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM