【问题标题】:How to re-sample pandas data-frame containing categorical data?如何重新采样包含分类数据的熊猫数据框?
【发布时间】:2017-03-19 21:37:13
【问题描述】:

我有一个带有列的数据框:
- 机器:[机器 1、机器 2、机器 3、机器 4、机器 5、机器 6]
- 时间:日期时间 27102016 00:00:00 - 00:59:00(每分钟)
- OutX:整数
- OutY:整数

数据框包含时间列中时间范围内所有机器的 OutX 和 OutY 读数。

yeild = pd.read_csv("d:/xdata/question.csv",parse_dates=True, index_col='Time')
yeild.dtypes

机器对象
OutX int64
OutY int64
数据类型:对象

yeild.describe()

...........OutX OutY
计数 358.000000 358.000000
平均 69.782123 70.715084
标准 9.685677 12.451468
最小 64.000000 66.000000
25% 67.000000 68.000000
50% 68.000000 69.000000
75% 70.000000 70.000000
最大 202.000000 246.000000

我想重新采样数据,以便可以在重新采样的数据帧中观察到 OutX 和 OutY 的最大值(我们称之为样本)。

sample = yeild.resample('10t').max()

采样后我只得到六条记录,因为它只接受所有 OutX 和 OutY 的最大值。
如何重新采样保持不同的分类值( Machine1,Machine2 ... Machine6)保持不变?即,每 10 分钟获取所有考虑中的机器的最大 OutX 和 OutY。
提前致谢!

【问题讨论】:

    标签: pandas jupyter-notebook


    【解决方案1】:

    这是未经验证的,因为您没有提供任何示例数据,而且我不想编造数据……我希望它有效

    您想知道max在哪里,然后用这些位置分割yield。您可以通过idxmax

    找到位置
    # where OutX is max within each 10 minutes
    mxX = yeild.resample('10t').OutX.idxmax()
    # where OutY is max within each 10 minutes
    mxY = yeild.resample('10t').OutY.idxmax()
    # union of mxX and mxY
    idx = mxX.append(mxY, ignore_index=True).unique()
    
    # slice yeild (sic) with locations of where
    # either OutY or OutX is max
    sample = yeild.loc[idx]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 2018-03-26
      • 2020-11-18
      • 1970-01-01
      • 2016-03-19
      • 2018-07-05
      • 2022-01-12
      相关资源
      最近更新 更多