【问题标题】:Pandas pivot table with very large number of columns具有大量列的 Pandas 数据透视表
【发布时间】:2019-08-15 20:45:12
【问题描述】:

我有一个 pandas 数据框df,大约有 1000 行但有 500 列。这些列被命名为 Run1、Run2、...、Run500

现有索引为datetime

dataframe中的样本数据如下:

df.ix[1:4,1:4]
                       Run1    Run2    Date
2019-04-01 01:00:00  23.0263  23.0263  2019-04-01
2019-04-01 01:00:00  19.2212  19.2212  2019-04-01
2019-04-02 01:00:00  19.3694  19.3694  2019-04-02
2019-04-02 01:00:00  19.3694  19.3694  2019-04-02

我可以尝试以下方法:

pd.pivot_table(df, index=['Date'], values=['Run1'], aggfunc=[np.mean])['mean']

但我需要以下:

import pandas as pd
import numpy as np
pd.pivot_table(df, index=['Date'], values=['Run1', 'Run2', ...., 'Run500'], aggfunc=[np.mean])['mean']

【问题讨论】:

    标签: python python-3.x pandas python-3.5


    【解决方案1】:

    我想这是groupby + mean

    df.groupby('Date').mean()
    

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多