8-Bit Borges
8-Bit Borges

Reputation: 10033

Pandas -- convert large numbers from string to numeric

I have this df, where 'escalacoes' values are currently as type str:

            apelido escalacoes
0              Keno  2.868.755
1             Pedro  2.483.069
2    Bruno Henrique  1.686.894
3        Hugo Souza    809.186
4   Guilherme Arana  1.314.769
5       Filipe Luís    776.147
6   Thiago Galhardo  2.696.853
7          Vinícius  1.405.012
8              Nenê  1.369.209
9    Jorge Sampaoli  1.255.731
10            Réver  1.505.522
11    Víctor Cuesta  1.220.451

Now I'm trying to convert those string values to numeric and then sort them. I'm trying:

df.escalacoes = pd.to_numeric(df.escalacoes, errors='coerce')

df = df.sort_values('escalacoes', ascending=False).reset_index(drop=True).copy()

But I'm getting:

0        Hugo Souza     809.186
1       Filipe Luís     776.147
2              Keno         NaN
3             Pedro         NaN
4    Bruno Henrique         NaN
5   Guilherme Arana         NaN
6   Thiago Galhardo         NaN
7          Vinícius         NaN
8              Nenê         NaN
9    Jorge Sampaoli         NaN
10            Réver         NaN
11    Víctor Cuesta         NaN

Apparently, pandas does not know how to convert string numbers in the millions to numeric.


How do I achieve this?

Upvotes: 0

Views: 133

Answers (1)

Zero
Zero

Reputation: 76917

You'd have to replace the . dots in the strings first. If you're certain your data doesn't have any other issues you could then simply use .astype(int) or pd.to_numeric(.., errors='coerce')

In [25]: df.escalacoes = df.escalacoes.str.replace('.', '').astype(int) / 1000

In [26]: df.sort_values('escalacoes', ascending=False, ignore_index=True)
Out[26]: 
                 apelido  escalacoes
0                   Keno    2868.755
1        Thiago Galhardo    2696.853
2                  Pedro    2483.069
3         Bruno Henrique    1686.894
4                  Réver    1505.522
5               Vinícius    1405.012
6                   Nenê    1369.209
7        Guilherme Arana    1314.769
8         Jorge Sampaoli    1255.731
9          Víctor Cuesta    1220.451
10            Hugo Souza     809.186
11           Filipe Luís     776.147

Upvotes: 1

Related Questions