首页 > 解决方案 > 熊猫,圆形 - 功能

问题描述

在 pandas 数据帧上运行 round - 函数无效。

  1. 为什么会这样?
  2. 更一般的方法是什么?
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)

失败:控制台打印两次完全相同。

标签: pandas

解决方案


对我来说,在上一个熊猫版本中0.23.4它工作得很好,替代解决方案是使用dictionary

df_round=df_round.round({'time':1,'ph_1':0,'ph_2':2})
print(df_round)
      index  time  ph_1   ph_2 text
0  0.000000  10.3   8.0  17.95  foo
1  2.563624  20.5  13.0   7.69  bar
2  5.127248  15.4  21.0  20.51  spa
3  0.000000  10.3   8.0  17.95    m

推荐阅读