I'm doing a personal project to practice pandas, and Beautiful soup, I scraped this info and have it in a pandas df like this:
0 €8.5M
1 €0
2 €9.5M
3 €2M
4 €21M
...
16534 €1.8M
16535 €1.1M
16536 €550K
16537 €650K
16538 €1.1M
Name: Value, Length: 16539, dtype: object
0 €67K
1 €0
2 €15K
3 €11K
4 €13K
...
16534 €3K
16535 €2K
16536 €2K
16537 €7K
16538 €3K
Name: Wage, Length: 16539, dtype: object
So in order to analyse this info I want to clean and transform this data into integers, what I could come up with was this:
df['Wage'] = df['Wage'].apply(lambda x: re.sub('€','',x))
df['Wage'] = df['Wage'].apply(lambda x: re.sub('K','000',x))
df['Value'] = df['Value'].apply(lambda x: re.sub('€','',x))
df['Value'] = df['Value'].apply(lambda x : re.sub('M','00000',x) if (('M' in x) and ('.' in x))else x)
df['Value'] = df['Value'].apply(lambda x : re.sub('[.]','',x))
df['Value'] = df['Value'].apply(lambda x : re.sub('M','000000',x))
df['Value'] = df['Value'].apply(lambda x : re.sub('K','000',x))
df['Wage'] = df['Wage'].astype(int)
df['Value'] = df['Value'].astype(int)
I replaced first the money sign, then check for dots so I can replace the M for 5 zeros, then the remaining M's for 6 zeros, then the K's for 3 zeros and then I do the type change into int. But I feel like this is not a good way of doing this, What do you think? What would be a better way of doing this? I tried creating a function but couldn't do it.