【问题标题】:How to use iloc slicing on outermost level in multiindex DataFrame?如何在多索引 DataFrame 的最外层使用 iloc 切片?
【发布时间】:2020-06-16 13:12:44
【问题描述】:

我有一个 Multiindex DataFrame,其中 pd.timestamp 作为顶级索引,一些任意参数作为二级索引。你可以使用这个 sn-p 来重现一些模拟数据:

level0 = pd.period_range(start='2020-01-01-00:00:00', end='2020-01-01-00:01:00', freq='s')
level1 = ['foo', 'bar', 'foobar']
values = np.random.rand(len(level1)*len(level0))
idx = pd.MultiIndex.from_product([level0, level1], names=['time', 'level1'])
col = ['value']
df = pd.DataFrame(values, idx, col)
print(df)

创建输出:

                               value
time                level1          
2020-01-01 00:00:00 foo     0.345507
                    bar     0.147654
                    foobar  0.617000
2020-01-01 00:00:01 foo     0.430975
                    bar     0.783075
...                              ...
2020-01-01 00:00:59 bar     0.027083
                    foobar  0.553220
2020-01-01 00:01:00 foo     0.253957
                    bar     0.569881
                    foobar  0.976768

现在我想将每 n 秒作为数据用于我的进一步计算。我的第一种方法是使用.iloc[::n],如果先将数据框拆开,则效果很好。所以代码df.unstack().iloc[::5].stack() 产生了我想要的输出:

                               value
time                level1          
2020-01-01 00:00:00 bar     0.147654
                    foo     0.345507
                    foobar  0.617000
2020-01-01 00:00:05 bar     0.083129
                    foo     0.591585
                    foobar  0.660372
2020-01-01 00:00:10 bar     0.460798
                    foo     0.308138
                    foobar  0.622412
...                              ...
2020-01-01 00:00:55 bar     0.700964
                    foo     0.556782
                    foobar  0.601582
2020-01-01 00:01:00 bar     0.569881
                    foo     0.253957
                    foobar  0.976768

但是,当 df 变大时,unstacking 和 stacking 会占用大量资源。而且我觉得有一个简单、优雅且“便宜”的解决方案,我就是无法掌握。

是否有一种解决方案可以选择每个第 n 个时间戳来避免数据拆分?

编辑:对于阅读此内容的任何人:虽然 jezraels 的回答是一个很好的方法,并且就如何处理问题向我提出了一些建议,但事实证明,对于我的情况(每月的第二次数据和 ca 300 “level1”-entries),unstack().iloc[::n].stack() 方法更快且可扩展性更好。

【问题讨论】:

    标签: python pandas slice


    【解决方案1】:

    按第一级选择值,然后对每5个值进行切片,最后传递给DataFrame.loc:

    df1 = df.loc[df.index.levels[0][::5]]
    print(df1)
    
                                   value
    time                level1          
    2020-01-01 00:00:00 foo     0.350853
                        bar     0.998113
                        foobar  0.077340
    2020-01-01 00:00:05 foo     0.029292
                        bar     0.394105
                        foobar  0.375882
    2020-01-01 00:00:10 foo     0.878306
                        bar     0.152500
                        foobar  0.299017
    2020-01-01 00:00:15 foo     0.821039
                        bar     0.298991
                        foobar  0.496110
    2020-01-01 00:00:20 foo     0.523729
                        bar     0.928747
                        foobar  0.902535
    2020-01-01 00:00:25 foo     0.128496
                        bar     0.126517
                        foobar  0.802517
    2020-01-01 00:00:30 foo     0.539462
                        bar     0.754518
                        foobar  0.243328
    2020-01-01 00:00:35 foo     0.159771
                        bar     0.058187
                        foobar  0.508651
    2020-01-01 00:00:40 foo     0.847019
                        bar     0.688022
                        foobar  0.368563
    2020-01-01 00:00:45 foo     0.575865
                        bar     0.531090
                        foobar  0.756400
    2020-01-01 00:00:50 foo     0.584265
                        bar     0.155704
                        foobar  0.734554
    2020-01-01 00:00:55 foo     0.035894
                        bar     0.047678
                        foobar  0.746624
    2020-01-01 00:01:00 foo     0.254821
                        bar     0.756032
                        foobar  0.694809
    

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 2020-06-22
      • 2017-01-15
      • 1970-01-01
      • 2023-01-10
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多