2

I have a dataframe :

A B 10.1 33.3 11.2 44.2s 12.3 11.3s 14.2s * 15.4s nan

i want output as

A B 10.1 33.3 11.2 44.2 12.3 11.3 14.2 0 15.4 0

How do I remove these tailing alphabets I have tried this code

1st approch: 

bulb_temp_df['A'].str.extract('(\d)').astype(float) bulb_temp_df['B'].str.extract('(\d)').astype(float)

2nd approch:

bulb_temp_df['A'] = bulb_temp_df['A'].astype(str) bulb_temp_df['A'] = bulb_temp_df['A'].map(lambda x: x.rstrip('aAbBcC'))

These are not working. They are not removing the tailing s from the columns.

PriyalChaudhari
  • 363
  • 1
  • 7
  • 23
  • I still havent found the solution for this tried doing this `bulb_temp_df[cols]=bulb_temp_df[cols].apply(lambda x:x.str.extract('(\d+\.\d+)',expand=False) .astype(float) .fillna(0))` – PriyalChaudhari Jun 22 '17 at 23:27

1 Answers1

1

You can extract floats first and for replace NaNs to 0 add fillna.

Solution is in apply for process multiple columns.

cols = ['A','B']

#if mixed values - numeric with strings
bulb_temp_df[cols]=bulb_temp_df[cols].astype(str)

bulb_temp_df[cols]=bulb_temp_df[cols].apply(lambda x:x.str.extract('(\d+\.\d+)',expand=False)
                                                      .astype(float)
                                                      .fillna(0))
      A     B
0  10.1  33.3
1  11.2  44.2
2  12.3  11.3
3  14.2   0.0
4  15.4   0.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • It return `NaN`, so function `fillna(0)` replace it. Thank you for accepting. You can upvote too - click to small triangle above `0` above accepting mark. Thanks. – jezrael Jun 22 '17 at 08:04
  • sorry for confusion it is removing the 's' from the digit but the numbers which does not have alphabet it is making them 0. can you please rectify on this – PriyalChaudhari Jun 22 '17 at 23:18
  • Check answer, use `bulb_temp_df[cols]=bulb_temp_df[cols].astype(str)` – jezrael Jun 23 '17 at 00:02
  • If i have some negative values in the column then it is not consodering those values those values are vanished . so itried this `'(\-\d+\.\d+)'` but then it will not consider positive values what to do in this case. I came acroos negative value and it is failing – PriyalChaudhari Jun 23 '17 at 01:24
  • I think you can use `'([-+]?\d*\.\d+|\d+)'`, or omit + like `'([-]?\d*\.\d+|\d+)'` – jezrael Jun 23 '17 at 04:22