如果满足某些条件,则比较整行是否相等

2024-09-26 22:52:00 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我在CSV文件中有以下匹配数据:

name,match1,match2,match3
Alice,2,4,3
Bob,2,3,4
Charlie,1,0,4

我正在写一个python程序。在我的程序的某个地方,我收集了一个存储在列表中的匹配的分数,比如x = [1,0,4]。我用熊猫找到了这些分数在数据中的位置,我可以打印“发现”或“未发现”。但是,我希望我的代码打印出这些分数对应的name。在这种情况下,程序应该输出“charlie”,因为charlie具有所有这些值[1,0,4]。我该怎么做

我将有一个大的数据集,所以我必须能够告诉哪个名称对应于我传递给程序的数字


Tags: 文件csv数据name程序列表地方分数
3条回答

是的,以下是如何比较数据帧中的整行:

df[(df == x).all(axis=1)].index   # where x is the pd.Series we're comparing to

此外,如果您在读取CSV时直接将name设置为索引列,则会使生活变得最简单

import pandas as pd
from io import StringIO

df = """\
name,match1,match2,match3
Alice,2,4,3
Bob,2,3,4
Charlie,1,0,4"""

df = pd.read_csv(StringIO(df), index_col='name')

x = pd.Series({'match1':1, 'match2':0, 'match3':4})

现在您可以看到,执行df == x或等效的df.eq(x)并不是您想要的,因为它执行元素级比较并返回一行True/False。因此,您需要使用.all(axis=1)来聚合这些行,这将发现所有比较结果都是True的行

df.eq(x).all(axis=1)

df[ (df == x).all(axis=1) ]
#         match1  match2  match3
# name
# Charlie       1       0       4

…最后,因为您只需要这些行的name

df[ (df == x).all(axis=1) ].index
# Index(['Charlie'], dtype='object', name='name')

df[ (df == x).all(axis=1) ].index.tolist()
# ['Charlie']

这正是你想要的。(为了清晰起见,我只在表达式中添加了空格)

您需要使用DataFrame.loc,其工作原理如下:

print(df.loc[(df.match1 == 1) & (df.match2 == 0) & (df.match3 == 4), 'name'])

也许可以试试这样:

import pandas as pd
import numpy as np

# Makes sample data
match1 = np.array([2,2,1])
match2 = np.array([4,4,0])
match3 = np.array([3,3,4])
name = np.array(['Alice','Bob','Charlie'])
df = pd.DataFrame({'name': id, 'match1': match1, 'match2':match2, 'match3' :match3})
df

# example of the list you want to get the data from
x=[1,0,4]
#x=[2,4,3]

# should return the name Charlie as well as the index (based on the values in the list x) 
df['name'].loc[(df['match1'] == x[0]) & (df['match2'] == x[1]) & (df['match3'] ==x[2])]

# Makes a new dataframe out of the above 
mydf = pd.DataFrame(df['name'].loc[(df['match1'] == x[0]) & (df['match2'] == x[1]) & (df['match3'] ==x[2])])

# Loop that prints out the name based on the index of mydf
# Assuming there are more than one name, it will print all. if there is only one name, it will print only that)
for i in range(0,len(mydf)):
      print(mydf['name'].iloc[i])

相关问题 更多 >

    热门问题