2017-06-21 120 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