如何在pandas数据框中过滤行,其中列的值等于列表的某个值

时间:2018-11-16 15:56:55

标签: python pandas dataframe pandas-groupby

我有一个包含两列的数据框:一列用于ID_number,一列用于week_number。 看起来可能像这样:

df1 = pd.DataFrame({'ID_number':[13, 13, 14, 14, 14, 15, 15,16], 'week_number':[1, 2, 1, 2, 3, 1, 4, 5]})

#   ID_number   week_number
#0  13  1
#1  13  2
#2  14  1
#3  14  2
#4  14  3
#5  15  1
#6  15  4
#7  16  5

我想为每个不同的ID选择星期值为2和3的那些ID,然后为数据做一个标签。如果一个ID没有第2周和第3周,则将其标记为1。否则,将其标记为0。

就目前而言,我提出了一个不太优雅的解决方案,该方法可行,但我确信必须有另一种方法:

def check_courier_week(df, field, weeks):
    weeks_not_provided = weeks
    new_df = df
    new_df['label'] = np.zeros(len(df))
    for c in np.unique(df[field]):
        tmp = df[df[field] == c]
        if len(np.unique(tmp.week_number.isin(weeks_not_provided))) == 1 and np.unique(np.unique(tmp.week_number.isin(weeks_not_provided))) == False:
            new_df['label'][df[field] == c] = 1
        else:
            new_df['label'][df[field] == c] = 0
    return new_df

有关如何改善这种情况的任何想法?我猜可能有一个使用groupby的解决方案,但我不知道如何实现。

生成的标签应为:

#   ID_number   week_number     label
#0  13  1   0.0
#1  13  2   0.0
#2  14  1   0.0
#3  14  2   0.0
#4  14  3   0.0
#5  15  1   1.0
#6  15  4   1.0
#7  16  5   1.0

谢谢!

4 个答案:

答案 0 :(得分:2)

groupbytransform any一起使用

(~(df1['week_number'].isin([2,3])).groupby(df1['ID_number']).transform('any')).astype(int)
Out[39]: 
0    0
1    0
2    0
3    0
4    0
5    1
6    1
7    1
Name: week_number, dtype: int32

答案 1 :(得分:1)

使用isinnp.where而不进行分组:

unique = df1.loc[df1['week_number'].isin([2,3]), 'ID_number'].unique()
df['label'] = np.where(df1['ID_number'].isin(unique), 0, 1)

或者:

df['label'] = (~df1['ID_number'].isin(unique)).astype(int)

print(df)
   ID_number  week_number  label
0         13            1      0
1         13            2      0
2         14            1      0
3         14            2      0
4         14            3      0
5         15            1      1
6         15            4      1
7         16            5      1

答案 2 :(得分:1)

虽然效率不高,但您可以通过set.isdisjoint利用set操作:

def checker(x):
    return set(x).isdisjoint({2, 3})

df1['flag'] = df1.groupby('ID_number')['week_number'].transform(checker)

print(df1)

   ID_number  week_number  flag
0         13            1     0
1         13            2     0
2         14            1     0
3         14            2     0
4         14            3     0
5         15            1     1
6         15            4     1
7         16            5     1

答案 3 :(得分:0)

要回答如何使用groupby:您可以按ID_number进行分组,然后以这种方式找到标签,即IE:

df1['label'] = np.zeros(len(df))
grouped_table = df1.groupby('ID_number')
groups = list(set(df1['ID_number']))
for group in groups:
    test_list = list(set(grouped_table.getgroup(group)))
    if (2 in test_list) & (3 in test_list):
        df1.loc[df1['ID_number'] == group]['label'] = 0  
    else:
        df1.loc[df1['ID_number'] == group]['label'] = 1