如何在groupby pandas之后删除多索引

时间:2018-03-26 14:32:59

标签: python pandas multi-index hierarchical

我开始时:

                     DISCH_PRESS  stage
timestamp                              
2017-11-17 07:10:09  6241.483887   14.0
2017-11-17 07:10:12  6353.102051   14.0
2017-11-17 07:10:15  6673.465820   14.0
2017-11-17 07:10:17  7089.970215   14.0
2017-11-17 07:10:20  7449.266113   14.0

我执行了以下操作,以便在每个阶段分组后重新采样时间戳:

df = df.groupby(['stage']).resample('1S').asfreq()

现在我的数据如下:

                           DISCH_PRESS  stage
stage timestamp                              
1.0   2017-11-18 23:18:20  4728.775879    1.0
      2017-11-18 23:18:21          NaN    NaN
      2017-11-18 23:18:22          NaN    NaN
      2017-11-18 23:18:23  5109.784180    1.0
      2017-11-18 23:18:24          NaN    NaN

我想删除舞台行索引。我的数据应如下所示:

                     DISCH_PRESS  stage
timestamp                              
2017-11-18 23:18:20  4728.775879    1.0
2017-11-18 23:18:21          NaN    NaN
2017-11-18 23:18:22          NaN    NaN
2017-11-18 23:18:23  5109.784180    1.0
2017-11-18 23:18:24          NaN    NaN

当我尝试搜索解决方案时,我主要找到了多列索引的解决方案,我尝试为我的应用程序调整这些解决方案,请参阅以下输入和产生的错误:

df.MultiIndex.droplevel(level=0)
AttributeError: 'DataFrame' object has no attribute 'MultiIndex'

df.droplevel(level=0)
AttributeError: 'DataFrame' object has no attribute 'droplevel'

dframe.reset_index()
ValueError: cannot insert timestamp, already exists

df.rows.droplevel()
AttributeError: 'DataFrame' object has no attribute 'row'

显然我错误地使用了这些方法。任何帮助表示赞赏。

0 个答案:

没有答案