python - 需要帮助对数据进行排序

标签 python python-3.x

我正在尝试为计算生物学研究项目清理一些数据。然而,一个问题出现了,一些同一天同一窝出生的狗有同一个母亲,但有多个父亲。我需要找到这些数据点并将它们返回一些,以便我可以手动返回文档并检查它们。有谁知道更好的方法,让每组 Action 不需要 30 多分钟才能完成?

到目前为止,我一直在尝试使用 pandas 来浏览数据,而且我不是 CS 向导。我基本上使用 for 循环来单独检查数据,即使较小的集合也有大约 10k 条数据。

data = raw_data.loc[:,['Order', 'Name', 'Sire', 'Dam', 'Registration', 'DOB']]
length = len(data.index)

for i in range(0,length,1):
    for j in range(i+1,length,1):
        if (data.iat[i,5]==data.iat[j,5]): #Same date of birth
            if (data.iat[i,3]==data.iat[j,3]): #Same mother
                if (data.iat[i,2]!= data.iat[j,2]): #Different father
                    print(data.iat[i,0]+data.iat[j,0])

最佳答案

您可以按出生日期和母亲对数据进行分组,然后计算父亲列的不同值的数量。将为每组 DOB 和 Dam 计算结果。您将对结果大于 1 的所有组感兴趣。

import pandas as pd
data.groupby(by=['DOB','Dam']).\ # Group your data by 'DOB' and 'Dam'
aggregate({'Sire':pd.Series.nunique}).\ # Count distinct values for 'Sire' in each group
sort_values(by="Sire", ascending= False).\ # Descending order of the results
query("Sire > 1").\ # Take the 'DOB' and 'Dam' pairs with more than 1 'Sire'
to_excel("File_with_results.xlsx") # Write the results to an excel file

关于python - 需要帮助对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58386145/

相关文章:

python - 从现有 MySQL 数据库逆向工程 SQLAlchemy 声明性类定义?

python - 车身凹痕检测

python - functools.wraps 相当于类装饰器

python - 错误 "no exception supplied"是什么意思?

python - Python 中的 bool 搜索文本文件

python - Django RequestFactory 添加 HTTP_X_FORWARDED_FOR

python - 提取段落中与列表中的单词相似的单词

python - 为什么 sort() 函数不能应用于列表理解?

python - 使用 itertools.product 在范围内重复

Python 异常处理 - 避免编写 30+ try except block