【问题标题】:Pandas filtering floating point to digitPandas 将浮点数过滤为数字
【发布时间】:2018-03-17 03:38:34
【问题描述】:

我正在使用 Pandas 分析我的数据。我有这个由 elapsed_seconds 和 m(magnitude) 组成的数据框。有没有办法让我按浮点数的五位数(elapsed_seconds)分组并找到 m 的平均值?

例子:

elapsed_seconds,m
10769.001,0.373637934043
10769.027,0.373403294813
10769.041,0.373069383556
10769.061,0.391354911476
10769.081,0.381280413801

我需要的是:

elapsed_seconds,m
10769,0.3785491875378

0.3785491875378 是 0.373637934043,0.373403294813,0.373069383556,0.391354911476 和 0.381280413801 的平均值

我会感谢所有的反馈和评论。谢谢。

【问题讨论】:

    标签: python pandas dataframe mean pandas-groupby


    【解决方案1】:

    您可以将float 列转换为int,然后将groupby 和聚合mean

    df = df.groupby(df['elapsed_seconds'].astype(int))['m'].mean().reset_index()
    print (df)
       elapsed_seconds         m
    0            10769  0.378549
    

    【讨论】:

    • 谢谢@jezrael先生。正是我需要的。
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2014-02-13
    • 2017-11-29
    • 2020-02-08
    相关资源
    最近更新 更多