Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dataframe objects to float by iterating over columns

I want to convert data in Pandas.Series by iterating over Series

DataFrame df looks like

   c1   c2
0  -    75.0%
1 -5.5% 65.8%
.
n  -    6.9%

'%' and '-' only values should be removed. Desired result:

   c1    c2
0  0.0   75.0
1 -5.5   65.8
.
n  0.0    6.9

If I call

df['c1']= df['c1'].str.replace('%', '')
df['c1']= df['c1'].str.replace('-%', '0')
df['c1']= df['c1'].astype(float)

it works.

But if I try to iterate it does not:

col= [] 
col.append(df['c1'])
col.append(df['c2'])

for i  in (col):
    i = i.str.replace('%', '',regex=True)
    i = i.str.replace('-$', '0',regex=True)

Thanks in advance

like image 784
Bernd Blase Avatar asked Dec 22 '25 15:12

Bernd Blase


1 Answers

EDIT: Improved regex

# Thanks to @tdy
df.replace({'\%':'', r'^\s*-\s*$':0}, regex=True)

Explanation - Since string-based data can often have random spaces. also you can just replace it with 0 since the subsequent float conversion will handle the decimals.

Output

    c1      c2
0   0.0     75.0
1   -5.5    65.8
2   0.0     6.9

Explanation

We can use regex over complete df, to replace the required symbols, we are replacing % with empty string and if a row consists of - at the end then replace it with 0.0.

like image 100
Utsav Avatar answered Dec 24 '25 04:12

Utsav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!