如果一行满足特定条件,则在多索引数据框中选择整个子组

时间:2019-06-25 11:48:04

标签: python pandas dataframe

如果该子集中的某一行满足条件,我想在多索引数据框中选择一个子组。这是一个解释我的问题的简单数据框:

col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col3 = ['T','F','F','F','F','F','F','T','F','F','F','F','F', 'T']

d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6], 
 'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017], 'col1' : col1, 'col2' : col2 }
df = pd.DataFrame(data=d)

new_df = df.groupby(['Unit', 'Year']).sum()

new_df['col3'] = (new_df.groupby(level=0, group_keys=False)
                  .apply(lambda x: x.col1/x.col2.shift())
                 )

           col1  col2      col3
Unit Year                      
1    2014     0     0       T
     2015     0     0       F
     2016     0     0       F
     2017     0     0       F
2    2015     2     4       F
     2016     4     6       F
     2017     6     8       F
3    2017     0     0       T
4    2014     0     0       F
5    2015     0     0       F
6    2014   100   200       F
     2015   200   900       F
     2016   300   400       F
     2017   400   500       T


所以我想选择第3列中所有具有一个T的所有子组

所以我的输出如下:

           col1  col2      col3
Unit Year                      
1    2014     0     0       T
     2015     0     0       F
     2016     0     0       F
     2017     0     0       F
3    2017     0     0       T
6    2014   100   200       F
     2015   200   900       F
     2016   300   400       F
     2017   400   500       T

先谢谢您

Jen

1 个答案:

答案 0 :(得分:1)

使用:

col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col3 = ['T','F','F','F','F','F','F','T','F','F','F','F','F', 'T']

d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6], 
 'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017], 
         'col1' : col1, 'col2' : col2, 'col3' : col3 }
df = pd.DataFrame(data=d)

df = df.set_index(['Unit','Year'])

df = df[df['col3'].eq('T').astype(int).groupby(level=0).transform('sum').eq(1)]
print (df)
           col1  col2 col3
Unit Year                 
1    2014     0     0    T
     2015     0     0    F
     2016     0     0    F
     2017     0     0    F
3    2017     0     0    T
6    2014   100   200    F
     2015   200   900    F
     2016   300   400    F
     2017   400   500    T

详细信息

将列的相等性用Series.eq比较并转换为整数:

print (df['col3'].eq('T').astype(int))
Unit  Year
1     2014    1
      2015    0
      2016    0
      2017    0
2     2015    0
      2016    0
      2017    0
3     2017    1
4     2014    0
5     2015    0
6     2014    0
      2015    0
      2016    0
      2017    1
Name: col3, dtype: int32

然后以GroupBy.transform的数量计算每个第一级的sum,以得到相同的大小Series

print (df['col3'].eq('T').astype(int).groupby(level=0).transform('sum'))
Unit  Year
1     2014    1
      2015    1
      2016    1
      2017    1
2     2015    0
      2016    0
      2017    0
3     2017    1
4     2014    0
5     2015    0
6     2014    1
      2015    1
      2016    1
      2017    1
Name: col3, dtype: int32

1比较,最后按boolean indexing过滤:

print (df[df['col3'].eq('T').astype(int).groupby(level=0).transform('sum').eq(1)])
           col1  col2 col3
Unit Year                 
1    2014     0     0    T
     2015     0     0    F
     2016     0     0    F
     2017     0     0    F
3    2017     0     0    T
6    2014   100   200    F
     2015   200   900    F
     2016   300   400    F
     2017   400   500    T