python - 需要帮助对数据进行排序
问题描述
我正在尝试为计算生物学研究项目清理一些数据。然而,出现了一个问题,即同一窝同一窝出生的一些狗有相同的母亲,但有多个父亲。我需要找到这些数据点并将它们返回一些,以便我可以手动返回文档并检查它们。有谁知道更好的方法,使每组不需要 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
推荐阅读
- hbase - HBASE - 如何在 HBASE 中使用 SingleColumnValueFilter 应用“LIKE”过滤器操作?
- c# - 未调用 WPF DependencyProperty PropertyChangedCallback
- python - python正则表达式跳过它不应该的刺痛
- java - 如何解决“过时的元素引用:导航到上一页时元素未附加到页面文档”
- matlab - 如何从 MATLAB ode45 中提取参数向量?
- spring-security - Spring Boot 使用 CSRF 令牌和 BasicAuth 保护相同的端点
- javascript - 多个画布上的多个场景 Threejs
- sql-server - 为什么 DECIMAL 的行为类似于 FLOAT?
- applescript - AppleScript——列表 1 的项目包含列表 2 的项目
- android - Xamarin 图像背景未在 Android 上显示