在Pandas中,如何根据另一个数据框从数据框中删除行?

时间:2016-10-05 17:50:01

标签: python pandas

我有2个数据框,一个名为USERS,另一个名为EXCLUDE。他们俩都有一个名为" email"。

的字段

基本上,我想删除USERS中包含EXCLUDE中包含的电子邮件的每一行。

我该怎么做?

4 个答案:

答案 0 :(得分:17)

您可以使用boolean indexing并使用isin条件,反转布尔Series~

import pandas as pd

USERS = pd.DataFrame({'email':['a@g.com','b@g.com','b@g.com','c@g.com','d@g.com']})
print (USERS)
     email
0  a@g.com
1  b@g.com
2  b@g.com
3  c@g.com
4  d@g.com

EXCLUDE = pd.DataFrame({'email':['a@g.com','d@g.com']})
print (EXCLUDE)
     email
0  a@g.com
1  d@g.com
print (USERS.email.isin(EXCLUDE.email))
0     True
1    False
2    False
3    False
4     True
Name: email, dtype: bool

print (~USERS.email.isin(EXCLUDE.email))
0    False
1     True
2     True
3     True
4    False
Name: email, dtype: bool

print (USERS[~USERS.email.isin(EXCLUDE.email)])
     email
1  b@g.com
2  b@g.com
3  c@g.com

merge的另一个解决方案:

df = pd.merge(USERS, EXCLUDE, how='outer', indicator=True)
print (df)
     email     _merge
0  a@g.com       both
1  b@g.com  left_only
2  b@g.com  left_only
3  c@g.com  left_only
4  d@g.com       both

print (df.loc[df._merge == 'left_only', ['email']])
     email
1  b@g.com
2  b@g.com
3  c@g.com

答案 1 :(得分:1)

我的解决方案是找到共同的元素,提取共享密钥,然后使用该密钥将它们从原始数据中删除:

class newClass {
  public $property1;
  public $property2;
  public function NewFunction {
    return 'Something';
  }
}
$array = array();

for($i = 0; $i < 2; $i++ {
  $temp = new newClass;
  $temp -> property2 = 'SomeData';
  $array[$i] = $temp;
  $array[$i] -> property1 = 'Random';
  unset($temp);
}

答案 2 :(得分:1)

为了扩展 jezrael 的答案,可以使用相同的方法来过滤基于多列的行。

USERS = pd.DataFrame({"email": ["a@g.com", "b@g.com", "c@g.com", 
                                "d@g.com", "e@g.com"],
                      "name": ["a", "s", "d", 
                               "f", "g"],
                      "nutrient_of_choice": ["pizza", "corn", "bread", 
                                             "coffee", "sausage"]})

print(USERS)    

     email name nutrient_of_choice
0  a@g.com    a              pizza
1  b@g.com    s               corn
2  c@g.com    d              bread
3  d@g.com    f             coffee
4  e@g.com    g            sausage

EXCLUDE = pd.DataFrame({"email":["x@g.com", "d@g.com"],
                        "name": ["a", "f"]})

print(EXCLUDE)

     email name
0  x@g.com    a
1  d@g.com    f

现在,假设我们只想过滤具有匹配姓名和电子邮件的行:

USERS = pd.merge(USERS, EXCLUDE, on=["email", "name"], how="outer", indicator=True)

print(USERS)

     email name nutrient_of_choice      _merge
0  a@g.com    a              pizza   left_only
1  b@g.com    s               corn   left_only
2  c@g.com    d              bread   left_only
3  d@g.com    f             coffee        both
4  e@g.com    g            sausage   left_only
5  x@g.com    a                NaN  right_only

USERS = USERS.loc[USERS["_merge"] == "left_only"].drop("_merge", axis=1)

print(USERS)

     email name nutrient_of_choice
0  a@g.com    a              pizza
1  b@g.com    s               corn
2  c@g.com    d              bread
4  e@g.com    g            sausage

答案 3 :(得分:0)

您还可以使用内部联接,在USERS中获取带有电子邮件EXCLUDE的索引或行,然后将其从USERS中删除。下面,我使用@jezrael示例进行显示:

import pandas as pd
USERS = pd.DataFrame({'email': ['a@g.com',
                                'b@g.com',
                                'b@g.com',
                                'c@g.com',
                                'd@g.com']})

EXCLUDE = pd.DataFrame({'email':['a@g.com',
                                 'd@g.com']})

# rows in USERS and EXCLUDE with the same email
duplicates = pd.merge(USERS, EXCLUDE, how='inner',
                  left_on=['email'], right_on=['email'],
                  left_index=True)

# drop the indices from USERS
USERS = USERS.drop(duplicates.index)

此返回:

USERS
    email
2   b@g.com
3   c@g.com
4   d@g.com