python - 打印具有最大小数点的列的值
问题描述
这是我的数据框:
col1 col2
0 12.13 13.13
1 100.133 12.19994
2 11.16664 140.13
3 9.13 2.13
4 3.23 10.13
现在我想要具有最大小数点长度的列值。
**OUTPUT:**
maximum_de_point
**COL1** 11.16664
**COL2** 12.19994
解决方案
一种选择是用 拆分值str.split
,取str.len
小数部分的 并找到idxmax
每列的 。然后lookup
使用结果值:
df_ixmax = df.astype(str).apply(lambda x: x.str.split('.').str[1].str.len()).idxmax(0)
df_ixmax[:] =df.lookup(*df_ixmax.reset_index().values[:,::-1].T)
df_ixmax
col1 11.16664
col2 12.19994
dtype: float64
或者我们也可以使用decimal.Decimal
,它可以通过返回的命名元组 by 获得小数位数as_tuple()
,然后从结果中类似上面的索引数据帧:
from decimal import Decimal
ix = [[Decimal(str(x)).as_tuple().exponent for x in col] for col in df.values.T]
max_vals = df.values[np.array(ix).argmin(1), np.arange(df.shape[1])]
pd.Series(max_vals, index=df.columns)
col1 11.16664
col2 12.19994
dtype: float64
推荐阅读
- python - Pygame在未按下键时读取多个keydown事件?
- python - 尽管使用了过滤器,但 Pyarrow ParquetDataset.read() 在配置单元分区的 S3 数据集上很慢
- database - 实体-关系模型三元关系
- javascript - Amazon Connect 出站 CCP 软件电话号码预填充
- python - 将数据添加到 Django 数据库中的多对多关系
- mocha.js - Allure 测试报告未获取执行测试的正确设备名称
- python - Python中有没有办法获得以下输出?
- bazel - bazel如何选择python版本?
- c# - 复制 Excel 工作表的最有效方法是什么?
- python - 如何在flask\jinja 环境中将数据传递给javascript?