简体   繁体   中英

style.hide_index() adds multiple zeros after decimal

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?

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

Since pandas.io.formats.style.Styler.hide_index is deprecated, use pandas.io.formats.style.Styler.hide instead.

Deprecated since version 1.4.0 : This method should be replaced by hide(axis="index", **kwargs)

Try this:

df.style.format({'Transfer Fee (EUR)': "{:.2f}"}).hide()  

# Ouput:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM