[英]Printing out a list for specific elements in a table
I have two list x=[1,2,5,4,3]
y=[4,8,9,2,18]
and a .csv table that looks like the one below.我有两个列表
x=[1,2,5,4,3]
y=[4,8,9,2,18]
和一个 .csv 表,如下所示。
ID ![]() |
Age![]() |
Group![]() |
Name![]() |
---|---|---|---|
1 ![]() |
4 ![]() |
3 ![]() |
Sam![]() |
2 ![]() |
50 ![]() |
1 ![]() |
Raj![]() |
3 ![]() |
18 ![]() |
9 ![]() |
John![]() |
My goal is to print a list of Group
for elements that are in (x,y) and (Id,Age)
.我的目标是为
(x,y) and (Id,Age)
元素打印Group
列表。 So for example: Since (1,4)
is in (x,y)
and also in (Id,Age)
, the list would contain 3
.例如:由于
(1,4)
在(x,y)
和(Id,Age)
,因此列表将包含3
。 For (3,18)
would be a similar case as it is in both (x,y)
and (Id, Age)
.对于
(3,18)
将是类似的情况,因为它在(x,y)
和(Id, Age)
。 So then my result would be a list of these numbers, [3,9]
.那么我的结果将是这些数字的列表
[3,9]
。
I tired doing result=df[df['ID'].isin(x), df['Age'].isin(y)]['Group']
but this get me anywhere.我厌倦了
result=df[df['ID'].isin(x), df['Age'].isin(y)]['Group']
但这让我在任何地方。 I am stuck on what to do next.我被困在下一步该做什么上。 Any help would appreciated.
任何帮助将不胜感激。
What you try to do seems to be an AND
, that is done with你尝试做的似乎是一个
AND
,这是用
df[df['ID'].isin(x) & df['Age'].isin(y)]
But with that data但是有了这些数据
df = pd.DataFrame([{'ID': 1, 'Age': 4, 'Group': 3, 'Name': 'Sam'},
{'ID': 2, 'Age': 50, 'Group': 1, 'Name': 'Raj'},
{'ID': 3, 'Age': 18, 'Group': 9, 'Name': 'John'},
{'ID': 3, 'Age': 19, 'Group': 9, 'Name': 'John'}])
x = [1, 2, 5, 4, 3]
y = [4, 8, 9, 19, 18]
It would give also the (3, 19)
line even is that isn't a pair它也会给出
(3, 19)
线,即使它不是一对
ID Age Group Name
0 1 4 3 Sam
2 3 18 9 John
3 3 19 9 John
You need to look by row, here's some try你需要按行查看,这里有一些尝试
pairs = list(zip(x, y))
result = df[pd.Series(zip(df['ID'], df['Age'])).isin(pairs)]['Group']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.