需要帮助对数据进行排序

问题描述 投票:1回答:2

我正在尝试为计算生物学研究项目清除一些数据。但是,出现了一个问题,即在同一天从同一窝产下的一些狗有相同的母亲但有多个父亲。我需要找到这些数据点并以某种形式返回它们,以便可以手动返回到文档并进行检查。有谁知道更好的方法,这样每套游戏都不需要30分钟以上的时间来完成?

到目前为止,我一直在尝试使用熊猫浏览数据,而且我不是CS向导。我基本上使用了for循环来分别检查每个数据,即使是较小的数据集也有大约1万条数据。

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])
python python-3.x
2个回答
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

0
投票

欢迎使用Stackoverflow。

Miguel之外的另一个建议。

为了进行测试,我将文件缩小为一个小样本,其中包括您正在处理的问题。您不希望在知道程序运行之前就浪费CPU时间。

BDS

© www.soinside.com 2019 - 2024. All rights reserved.