简体   繁体   English

从 Pandas MultiIndex 中的列进行条件切片

[英]Conditional Slicing from Columns in Pandas MultiIndex

I am trying to conditionally slice data from a multiindex based on column names as opposed to index.我正在尝试根据列名而不是索引有条件地从多索引中切片数据。 For example, I have the following MultiIndex Data frame:例如,我有以下 MultiIndex 数据框:

   203        204         205
  TIME VALUE TIME VALUE  TIME VALUE
0    1   bar  1.0   LH2  10.0   dog
1    2   baz  2.0   LOX  11.0   cat
2    3   foo  3.0   CH4  12.0   pig
3    4   qux  NaN   NaN  13.0   rat
4    5   qaz  NaN   NaN   NaN   NaN
5    6   qoo  NaN   NaN   NaN   NaN

(I essentially have measurement data (203, 204, etc) with a time and value, recorded using different sample rates. Thus, the number of rows will always be different. I am putting all data into a single MultiIndex since it can contain a varying number of rows.) (我基本上有带有时间和值的测量数据(203、204 等),使用不同的采样率记录。因此,行数总是不同的。我将所有数据放入一个 MultiIndex,因为它可以包含一个不同的行数。)

I want to select all data if TIME is > 3. The expected output would be the following:如果 TIME > 3,我想选择所有数据。预期的输出如下:

   203        204         205
  TIME VALUE TIME VALUE  TIME VALUE
0    4   qux  NaN   NaN  10.0   dog
1    5   qaz  NaN   NaN  11.0   cat
2    6   qoo  NaN   NaN  12.0   pig
3   NaN  NaN  NaN   NaN  13.0   rat
4   NaN  NaN  NaN   NaN   NaN   NaN
5   NaN  NaN  NaN   NaN   NaN   NaN

I tried using the query method but that only works on an index, not a column name.我尝试使用查询方法,但这只适用于索引,而不适用于列名。 I do not want to transpose the dataframe to use query.我不想转置数据框以使用查询。 I also tried using loc but never seemed to find a way to get what I am looking for.我也尝试使用 loc 但似乎从未找到一种方法来获得我正在寻找的东西。 I even looked into using xs but I don't think I can add conditional slicing with it.我什至考虑使用 xs 但我认为我不能用它添加条件切片。

I found this on SO but it doesn't include conditional slicing: Selecting columns from pandas MultiIndex我在 SO 上找到了这个,但它不包括条件切片: 从熊猫 MultiIndex 中选择列

Here is the code that I have been using to test this:这是我用来测试的代码:

import pandas as pd
import numpy as np

d1 = {'TIME': [1,2,3,4,5,6], 'VALUE': ['bar', 'baz', 'foo', 'qux', 'qaz', 'qoo']}
df1 = pd.DataFrame(data=d1)

d2 = {'TIME': [1,2,3], 'VALUE': ['LH2', 'LOX', 'CH4']}
df2 = pd.DataFrame(data=d2)

d3 = {'TIME': [10,11,12,13], 'VALUE': ['dog', 'cat', 'pig', 'rat']}
df3 = pd.DataFrame(data=d3)

df_list = [df1, df2, df3] 

pids = [203, 204, 205]

df_multi = pd.concat(df_list, axis=1, keys=list(zip(pids)))

print(df_multi)

# Slice all time columns
ALL = slice(None)
df_multi_2 = df_multi.loc[ALL, (ALL, 'TIME')]
print(df_multi_2)

# Condition based slicing - does not work
ALL = slice(None)
df_multi_3 = df_multi.loc[ALL, df_multi.loc[ALL,(ALL,'TIME')] > 3]
print(df_multi_3)

Let's try IndexSlice to slice the data:让我们尝试使用IndexSlice对数据进行切片:

from pandas import IndexSlice

mask = (df_multi.loc[:, IndexSlice[:,"TIME"]].gt(3)
    .reindex(df_multi.columns, axis=1)
    .groupby(level=0, axis=1)
    .transform('any')
)

df_multi.where(mask)

Output:输出:

   203        204         205      
  TIME VALUE TIME VALUE  TIME VALUE
0  NaN   NaN  NaN   NaN  10.0   dog
1  NaN   NaN  NaN   NaN  11.0   cat
2  NaN   NaN  NaN   NaN  12.0   pig
3  4.0   qux  NaN   NaN  13.0   rat
4  5.0   qaz  NaN   NaN   NaN   NaN
5  6.0   qoo  NaN   NaN   NaN   NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM