使用pandas根据特定条件选择行

时间:2016-11-20 15:37:40

标签: python pandas

我想返回包含所有列的行> 0或只有2012可以< 0

import pandas as pd
import numpy as np

df = pd.DataFrame( {
   'A': ['d','d','d','f','f','f','g','g','g','h','h','h'],
   'B': [5,5,6,7,5,6,6,7,7,6,7,7],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1],
   'S': [2012,2013,2014,2015,2016,2012,2013,2014,2015,2016,2012,2013]     
    } );

df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
print (df)

@jezrael,不完全是。我更改了数据框以更好地解释。在最终结果中,我需要所有列都是>的行。 0和列是> 0,除了2012年。那个可以是< 0.结果必须显示具有限定列的新df。因此,在下面的示例中,g是,否。

df = pd.DataFrame( {
   'A': ['d','d','d','d','d','d','g','g','g','g','g','g'],
   'B': [5,5,6,-7,5,6,-6,7,7,6,-7,7],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1],
   'S': [2012,2013,2014,2015,2016,2012,2012,2014,2015,2016,2012,2013]     
    } );

df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)

S  2012  2013  2014  2015  2016
A                              
d    13     6     7    -6     6
g   -11     8     8     8     7

EDITED Dataframe;

df = pd.DataFrame( {
   'A':  ['d','d','d','d','d','d','g','g','g','g','g','g',
    'k','k','k','k','k','k'],
   'B': [5,5,6,7,5,6,-6,7,7,6,-7,7,-8,7,-6,6,-7,50],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2],
   'S':   [2012,2013,2014,2015,2016,2012,2012,2014,2015,2016,2012,
        2013,2012,2013,2014,2015,2016,2014]     
    } );

df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
print (df)

S  2012  2013  2014  2015  2016
A                              
d    13     6     7     8     6
g   -11     8     8     8     7
k    -6     9     48     8    -5

2 个答案:

答案 0 :(得分:1)

我认为你可以使用双掩码一个用于比较行,一个用于列:

df = pd.DataFrame( {
   'A': ['d','d','d','f','f','f','g','g','g','g','h','h','h', 'f'],
   'B': [5,5,6,7,5,6,-6,7,7,7,6,7,7,2],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
   'S': [2012,2013,2014,2015,2016,2012,2012,2013,2014,2015,2016,2012,2013,2013]     
    } );

df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
print (df)
S  2012  2013  2014  2015  2016
A                              
d     6     6     7     0     0
f     7     3     0     8     6
g    -5     8     8     8     0
h     8     8     0     0     7
mask1 = df[2012] < 0
print (mask1)
A
d    False
f    False
g     True
h    False
Name: 2012, dtype: bool

mask2 = (df > 0).all()
print (mask2)
S
2012    False
2013     True
2014    False
2015    False
2016    False
dtype: bool

print (df.loc[mask1, mask2])
S  2013
A      
g     8
print (df[mask1])
S  2012  2013  2014  2015  2016
A                              
g    -5     8     8     8     0

print (df.loc[:,mask2])
S  2013
A      
d     6
f     3
g     8
h     8

编辑问题编辑:

mask1 = df[2012] < 0
print (mask1)
A
d    False
g     True
Name: 2012, dtype: bool

mask2 = (df.drop(2012, axis=1) > 0).all(axis=1)
print (mask2)
A
d    False
g     True
dtype: bool

print (df[mask1 & mask2])
S  2012  2013  2014  2015  2016
A                              
g   -11     8     8     8     7

答案 1 :(得分:0)

合并运算符并使用括号:

df[((df > 0).all(axis=1)) | (df[2012] < 0)]
Out[22]: 
Empty DataFrame
Columns: [2012, 2013, 2014, 2015, 2016]
Index: []