【发布时间】:2026-01-23 13:05:01
【问题描述】:
在 pandas 数据帧上运行 round 函数无效。
- 为什么会这样?
- 更通用的方法是什么?
import pandas as pd
df_round=pd.DataFrame({'index': {0: 0.0, 1: 2.563624, 2: 5.127248, 3: 0.0},
'time': {0: 10.254496, 1: 20.508992, 2: 15.381744, 3: 10.254496},
'ph_1': {0: 7.690872, 1: 12.81812, 2: 20.508992, 3: 7.690872},
'ph_2': {0: 17.945368, 1: 7.690872, 2: 20.508992, 3: 17.945368},
'text': {0: 'foo', 1: 'bar', 2: 'spa', 3: 'm'}})
print("Before\n")
print(df_round)
decimals = pd.Series([1, 0, 2], index=['time','ph_1','ph_2'])
df_round=df_round.round(decimals)
print("After\n")
print(df_round)
失败:控制台打印两次完全相同。
【问题讨论】:
-
添加了变量。代码现已完成
-
对我来说效果很好。
-
你的熊猫版本是什么?
-
是的,对我来说似乎也可以正常工作。
-
pd.__version__ '0.23.0'
标签: pandas