简体   繁体   English

根据相同 dataframe 列表中某些列的值从 pandas dataframe 中选择行?

[英]Selecting rows from a pandas dataframe based on the values of some columns in a list of the same dataframe?

Let's suppose, there is a dataframe:假设,有一个 dataframe:

df1 = 
   A  B   C
0  1  a  a1
1  2  b  b2
2  3  c  c3
3  4  d  d4
4  5  e  e5
5  6  f  f6

Created as:创建为:

a1 = [1,2,3,4,5,6]
a2 = ['a','b','c','d','e','f']
a3 = ['a1','b2','c3','d4','e5','f6']
df1 = pd.DataFrame(list(zip(a1,a2,a3)),columns=["A","B","C"])

Here, I am considering Columns A and B to be something like primary keys for this dataframe.在这里,我认为A列和B列类似于此 dataframe 的主键。 So, PK = ["A","B"] .所以, PK = ["A","B"]

I have another list, list1 = [[2,'b'],[5,'e']] , which is a subset of the dataframe df[PK] .我有另一个列表list1 = [[2,'b'],[5,'e']] ,它是 dataframe df[PK]的子集。

Is there any way I can get the rows corresponding to these primary key values inside the list from the dataframe df?有什么方法可以从 dataframe df 中获取与列表中这些主键值对应的行吗?

Something like: df1 = df[df[PK].values.isin(list1)] which doesn't work as I expect.像: df1 = df[df[PK].values.isin(list1)]这不像我预期的那样工作。

I would like to get an output df1 as:我想得到一个 output df1 为:

df1 =
   A  B   C
1  2  b  b2
4  5  e  e5

There are some similar questions, which I have gone through in this portal.有一些类似的问题,我在这个门户中已经解决了。 But none of them showed me how to select rows based on filter on multiple columns as mentioned above.但是他们都没有向我展示如何基于上面提到的多列过滤器来 select 行。 Thanks in advance.提前致谢。

Here is how you can use pandas.DataFrame.merge() :以下是如何使用pandas.DataFrame.merge()

import pandas as pd
a1 = [1,2,3,4,5,6]
a2 = ['a','b','c','d','e','f']
a3 = ['a1','b2','c3','d4','e5','f6']

df1 = pd.DataFrame(list(zip(a1,a2,a3)),columns=["A","B","C"])

PK = ["A","B"]

list1 = [[2,'b'],[5,'e']]

df2 = df1.merge(pd.DataFrame(list1,columns=PK),on=PK)

print(df2)

Output: Output:

   A  B   C
0  2  b  b2
1  5  e  e5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM