2017-06-21 118 views
2

我有一個從獨特的t_id和s_id表構建的熊貓數據框,我想從這個數據框中刪除所有s_id的country_date爲null的t_id的記錄。如何根據條件過濾熊貓數據幀?

數據,如:

t_id s_id country_date 
T1 S1 jan 
T1 S2 mar 
T2 S1 
T2 S2 
T3 S2 jan 
T3 S3 

結果:

t_id s_id country_date 
T1 S1 jan 
T1 S2 mar 
T3 S2 jan 
T3 S3 

我寫了下面的線,但是這是錯誤的:

raw_data.groupby("t_id").country_date.max().notnull() 

請你能提供過濾數據幀記錄的方式根據上述標準。同時,打印被過濾掉的t_id。

回答

3

使用isnullall

df.groupby('t_id').filter(lambda x: ~x.country_date.isnull().all()) 

如果這些空白 '',而不是南你可能需要:

df.replace('',pd.np.nan).groupby('t_id').filter(lambda x: ~x.country_date.isnull().all()) 

輸出:

t_id s_id country_date 
0 T1 S1   jan 
1 T1 S2   mar 
4 T3 S2   jan 
5 T3 S3   NaN 

而且,看被丟棄的那些ID:

df.groupby('t_id').filter(lambda x: x.country_date.isnull().all())['t_id'].unique() 

輸出:

array(['T2'], dtype=object) 
+0

喜scott..thanks再次:)和的方式來打印被丟棄的T_ID記錄?不是保留的那個.. – user3222101

+0

注意到,我只是刪除否定符號(〜)以獲得其他記錄並添加['t_id']。unique() –

+0

再次感謝:) – user3222101