pandas Panda Python - 将一列除以 100(然后四舍五入 2.dp)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43675014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:29:57  来源:igfitidea点击:

Panda Python - dividing a column by 100 (then rounding by 2.dp)

pythonpandas

提问by ScoutEU

I have been manipulating some data frames, but unfortunately I have two percentage columns, one in the format '61.72' and the other '0.62'.

我一直在处理一些数据框,但不幸的是我有两个百分比列,一个格式为“61.72”,另一个格式为“0.62”。

I want to just divide the column with the percentages in the '61.72' format by 100 then round it to 2.dp so it is consistent with the data frame.

我只想将具有 '61.72' 格式百分比的列除以 100,然后将其四舍五入为 2.dp,因此它与数据框一致。

Is there an easy way of doing this?

有没有简单的方法来做到这一点?

My data frame has two columns, one called 'A' and the other 'B', I want to format 'B'.

我的数据框有两列,一列名为“A”,另一列名为“B”,我想格式化“B”。

Many thanks!

非常感谢!

回答by jezrael

You can use divwith round:

你可以用divround

df = pd.DataFrame({'A':[61.75, 10.25], 'B':[0.62, 0.45]})
print (df)
       A     B
0  61.75  0.62
1  10.25  0.45

df['A'] = df['A'].div(100).round(2)
#same as
#df['A'] = (df['A'] / 100).round(2)
print (df)
      A     B
0  0.62  0.62
1  0.10  0.45