There is a df_compliance data frame:

name pass_info Иванов Петр Александрович 4 520 802 159 Петров Михаил Иванович 4501256807 Сидоров Дим Димыч 4308 506310 Куприн Салтык Алекс 4513 № 056779 Воркутов Михаил Петрович 4501152999 

It is required to bring the pass_info column into the same view in order to get the following:

  name pass_info Иванов Петр Александрович 4520802159 Петров Михаил Иванович 4501256807 Сидоров Дим Димыч 4308506310 Куприн Салтык Алекс 4513056779 Воркутов Михаил Петрович 4501152999 

Help me figure out how to do it ...

I try to do it through df_compliance ['pass_info']. apply ('', join), but it turns out nonsense :(

  • What type of df_compliance ['pass_info']? and the symbol inside the number is a typo or intentionally delivered? - Bogdan
  • When I opened the csv file, I transferred these columns to str, unfortunately, not a typo, so the "people" were filled in: ((((((( Pavel
  • Should the 'pass_info' column contain only numbers? - Bogdan
  • yes, only numbers without spaces and other signs - Pavel

3 answers 3

Use Series.str.replace () and pd.to_numeric () :

 In [14]: df['pass_info'] = pd.to_numeric(df['pass_info'].str.replace('\D', ''), errors='coerce') In [15]: df Out[15]: name pass_info 0 Иванов Петр Александрович 4520802159 1 Петров Михаил Иванович 4501256807 2 Сидоров Дим Димыч 4308506310 3 Куприн Салтык Алекс 4513056779 4 Воркутов Михаил Петрович 4501152999 In [16]: df.dtypes Out[16]: name object pass_info int64 # <---- NOTE! dtype: object 
  • Thank you very much! - Pavel

Regular season option:

 import re trim = re.compile("\D").sub('', s) df_compliance.pass_info.apply(trim) 
  • Thank you, also a very good option! - Pavel
  • +1, so more elegant than in my answer. - Xander
 trim = lambda ss: ''.join(s for s in ss if s.isdigit()) df_compliance.pass_info.apply(trim) 
  • Thank you very much! - Pavel