简体   繁体   English

style.hide_index() 在小数点后添加多个零

[英]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() :我想摆脱索引列,所以我添加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:我得到了我想要的,但是转会费一栏中的值突然在小数点后加了0:

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?有什么办法可以把它放回去,比如在第一个 dataframe 中?

If OP's goal is to round the values in the column Transfer Fee (EUR) , one can use pandas.DataFrame.round as如果 OP 的目标是四舍五入Transfer Fee (EUR)列中的值,则可以使用pandas.DataFrame.round作为

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或者,如果 OP 想要指定确切的小数位数,假设一个希望该数字为2 ,那么可以使用pandas.DataFrame.apply如下

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. 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) 1.4.0 版后已弃用:此方法应替换为 hide(axis="index", **kwargs)

Try this:尝试这个:

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

# Ouput: # 输出:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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