0

I am working with a pandas dataframe of football players. There is a column with the value of each player. The problem is the type of this column is an object and I want to convert it to float64. How can I do it? The variable is Release clause.

df_fifa['Release Clause']
0        €226.5M
1        €127.1M
2        €228.1M
3        €138.6M
4        €196.4M
          ...   
18202      €143K
18203      €113K
18204      €165K
18205      €143K
18206      €165K
Name: Release Clause, Length: 18207, dtype: object

I want to convert it to the complete number. E.g. €200M to 200.000.000 and €200k to 200.000.

I know the funcion should be

df_fifa['Release Clause'].astype(str).astype(int)

But first I should remove €, M & k. I tried this for removing but it didn't work

df_fifa['Release Clause'] = df_fifa['Release Clause'].replace("€","")

Anyone knows how to do it?

Thank you!

4
  • The object should have methods to extract the data. Commented Jan 3, 2022 at 14:33
  • 4
    What have you tried, why did it fail? Commented Jan 3, 2022 at 14:34
  • I tried this function df_fifa['Release Clause'].astype(str).astype(int) but obviously it didn't work. First I need to remove "€", "M" & "k" and I don't know how to do it. And I also need to differentiate M (those numbers should be x1.000.000) and k (those numbers should be x100.000). Commented Jan 3, 2022 at 14:47
  • I suggest editing the question if you want to add more information. You cannot format code in comments. SO users also like to see some effort - otherwise, they get the impression to be treated as a free coding service. Commented Jan 3, 2022 at 14:50

1 Answer 1

2

Convert your symbol , K and M to '', * 1e3 and * 1e6 and evaluate your expression with pd.eval:

mapping = {'€': '', 'K': ' * 1e3', 'M': ' * 1e6'}

df_fifa['Release Clause'] = \
    pd.eval(df_fifa['Release Clause'].replace(mapping, regex=True))
print(df_fifa)

# Output
       Release Clause
0         226500000.0
1         127100000.0
2         228100000.0
3         138600000.0
4         196400000.0
18202        143000.0
18203        113000.0
18204        165000.0
18205        143000.0
18206        165000.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.