简体   繁体   English

如何比较项目列表是否出现在python数据框的每一行中

[英]How to compare if list of items are present in each row of a dataframe in python

I have a data_file of size 88k rows with 76 columns. 我有一个大小为88k,76列的data_file

I want to compare if a list: subset = [40,49] is present in how many rows 我想比较列表: subset = [40,49]在多少行中存在

I am comparing one row at a time as shown below: 我一次比较一行,如下所示:

My Code: 我的代码:

counter=0
for row in data_file.itertuples():
    if all(np.isin(subset, row)):
        counter = counter+1
print('Total occurences of subset: ', subset, '= ', counter)
print('--------------------------')

Execution time: 6.6398055266834035 执行时间:6.6398055266834035

Is there a better way to compare all rows at a time and save some time. 是否有更好的方式一次比较所有行并节省一些时间。 I need to check may subsets so the time complexity of my code is high. 我需要检查可能的子集,所以我的代码的时间复杂度很高。

Thanks, 谢谢,

Gopi 戈皮

np.sum((data_file==subset[0]).any(axis=1) & (data_file==subset[1]).any(axis=1))

暂无
暂无

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

相关问题 如何将每一行数据框合并到python中的列表中 - How to merge each row of dataframe into a list in python 将一个数据帧中的每一行与 Python 中另一个数据帧中的每一行进行比较 - Compare each row in one dataframe to each row in another dataframe in Python 比较每行的数据框列中的元素 - Python - Compare elements in dataframe columns for each row - Python 如何将 dataframe 的每个单元格与 python 中的字典列表进行比较? - how to compare each cell of dataframe with list of dictionary in python? 如何将 dataframe 中行的每个值与之前行中的每个值与 python 进行比较? - How to compare each value of row in a dataframe with each value in the row before with python? 在Python中比较列表中的项目 - Compare items of list with each other in Python 如何迭代一个 dataframe 的每一行并与 Python 中的另一个 dataframe 中的行进行比较? - how to iterate each row of one dataframe and compare with rows in another dataframe in Python? 将每个 pandas 行与列表字典和 append 新变量与 dataframe 进行比较 - Compare each pandas row to a dictionary of list and append new variable to the dataframe 如何检查列表中的字典项? 蟒蛇 - How to check dictionary items are present in the list ? PYTHON 如何将每一行与所有其他行进行比较,如果相同,我将连接到一个新的数据帧? Python - How do I compare each row with all the others and if it's the same I concatenate to a new dataframe? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM