首页 > 解决方案 > 需要帮助对数据进行排序

问题描述

我正在尝试为计算生物学研究项目清理一些数据。然而,出现了一个问题,即同一窝同一窝出生的一些狗有相同的母亲,但有多个父亲。我需要找到这些数据点并将它们返回一些,以便我可以手动返回文档并检查它们。有谁知道更好的方法,使每组不需要 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])

标签: pythonpython-3.x

解决方案


您可以按出生日期和母亲对数据进行分组,然后计算父亲列的不同值的数量。将为每组 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

推荐阅读