pandas - 熊猫,圆形 - 功能
问题描述
在 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)
失败:控制台打印两次完全相同。
解决方案
对我来说,在上一个熊猫版本中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
推荐阅读
- bash - Bash 脚本 - 将参数分组为单个参数
- excel - 未完成数据的标准差
- python - 为 size 属性设置动画时从中心调整小部件的大小
- c# - C# 替换正则表达式匹配中的一些字符
- javascript - 如何将 Vue 实例挂载到尚未附加的 HTML 元素
- python - 如何停止 multiprocessing.Pool on Exception
- sequelize.js - How to run recursive queries with sequelize?
- c - 声明具有赋值的指针
- java - 尝试使用 saveAll() 保存列表。它不起作用,也没有抛出异常
- javascript - 节点js:发送get请求,保存响应