style.hide_index() adds multiple zeros after decimal

Viewed 17

Here was what I'm working on

df_top_50= df[['Rank', 'Player', 'Position', 'Age', 'Nationality', 'Club Left', 'Club Joined', 'Transfer Fee (EUR)']]

It returned

   Rank Player              Position    Age Nationality Club Left       Club Joined        Transfer Fee (EUR)
1   1   Antony              Forward     22  Netherlands Ajax Amsterdam  Manchester United   95.00
2   2   Wesley Fofana       Defender    21  England     Leicester City  Chelsea FC          80.40
3   3   Aurélien Tchouameni Midfielder  22  Monaco      AS Monaco       Real Madrid         80.00

I wanna get rid of the index column, so I add style.hide_index():

df_top_50= df[['Rank', 'Player', 'Position', 'Age', 'Nationality', 'Club Left', 'Club Joined', 'Transfer Fee (EUR)']].style.hide_index()

I got what I want, but the values in transfer fee column suddenly added 0s after decimal:

Rank    Player              Position   Age  Nationality Club Left       Club Joined       Transfer Fee (EUR)
1       Antony              Forward    22   Netherlands Ajax Amsterdam  Manchester United 95.000000
2       Wesley Fofana       Defender   21   England     Leicester City  Chelsea FC        80.400000
3       Aurélien Tchouameni Midfielder 22   Monaco      AS Monaco       Real Madrid       80.000000

Is there any way to put it back, like in the first dataframe?

2 Answers

If OP's goal is to round the values in the column Transfer Fee (EUR), one can use pandas.DataFrame.round as

df = df.round({'Transfer Fee (EUR)': 1})

Then the values of that column will be

0    95.0
1    80.4
2    80.0

Alternatively, if OP wants to specify the exact number of decimal places, assuming one wants that number to be 2, then one can use pandas.DataFrame.apply as follows

df['Transfer Fee (EUR)'] = df['Transfer Fee (EUR)'].apply('{0:.2f}'.format)

Then the values of that column will be

0    95.00
1    80.40
2    80.00
Related