仅显示pandas

时间:2017-09-30 07:35:24

标签: python pandas dataframe pandas-groupby

来自here

import pandas as pd

data = {'date': ['1998-03-01 00:00:01', '2001-04-01 00:00:01','1998-06-01 00:00:01','2001-08-01 00:00:01','2001-05-03 00:00:01','1994-03-01 00:00:01'], 
        'node1': [1, 1, 2,2,3,2],
     'node2': [8,316,26,35,44,56],
     'weight': [1,1,1,1,1,1], }
df = pd.DataFrame(data, columns = ['date', 'node1','node2','weight'])

df['date'] = pd.to_datetime(df['date'])

mask = df.groupby('node1').apply(lambda x : (x['date'].dt.year.isin([1998,1999,2000])).any())
mask2 = df.groupby('node1').apply(lambda x : (x['date'].dt.year.isin([2001,2002,2003])).any())


print df[df['node1'].isin(mask[mask & mask2].index)]

我需要的输出是年份范围(98-00)和(01-03)中的节点,但它也应该只显示两个范围内的行。

预期产出 -

node1          node2         date
1                8          1998-03-01
1               316         2001-04-01
2               26          1998-06-01
2              35           2001-08-01  

现在这段代码也在打印这一行:2 56 1994-03-01。

1 个答案:

答案 0 :(得分:3)

一个简单的解决方案是首先删除不在两个日期范围内的日期,然后应用掩码,即

l1 = [1998,1999,2000]
l2 = [2001,2002,2003]
ndf = df[df['date'].dt.year.isin(l1+l2)]

获得ndf后:

选项1:您可以采用基于双组掩码的方法,即

mask = ndf.groupby('node1').apply(lambda x : (x['date'].dt.year.isin(l1)).any())
mask2 = ndf.groupby('node1').apply(lambda x : (x['date'].dt.year.isin(l2)).any())

new =  ndf[ndf['node1'].isin(mask[mask & mask2].index)]

谢谢@Zero

选项2:您可以进行groupby转换

new = ndf[ndf.groupby('node1')['date'].transform(lambda x: x.dt.year.isin(l1).any() & x.dt.year.isin(l2).any())]

选项3: groupby过滤器

new = ndf.groupby('node1').filter(lambda x: x['date'].dt.year.isin(l1).any() & x['date'].dt.year.isin(l2).any())

输出:

                 date  node1  node2  weight
0 1998-03-01 00:00:01      1      8       1
1 2001-04-01 00:00:01      1    316       1
2 1998-06-01 00:00:01      2     26       1
3 2001-08-01 00:00:01      2     35       1