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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With