Python Pandas数据框中的行排序/计数

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

我正在尝试开发质量检查脚本,该脚本将检查一组数据(在熊猫数据框中)并计算不同类型样本的总数。这是来自数据库的示例:

enter image description here

有问题的样品都带有

XXX123

我当前的脚本选择并选择所有质量控制样本,如Blank或IRM,但是我很难计算出实际的XXX123,因为其中一些正在获取两种类型的重复项,作为内部质量检查。

  1. 一种是带有“ ORIG”和“ PREP”的类型
  2. 第二种类型带有“ .1”和“ .2”

另外的问题是,很少有一个样本会同时获得两个样本,就像您在XXX123 85-90上看到的一样

最后,问题是我怎么可能解释这个?如何告诉python这:

  • [只要一行包含“ .1”,而下面的一行包含“ .2”-将这两个条目计为1
  • [只要一行包含“ ORIG”,而下面的一行包含“ PREPDUP”-将这两个条目计为1
  • [只要有一行包含“ ORIG .1”,而下面的一行包含“ ORIG .2”,下面的第三个包含“ PREPDUP”-将这三个条目记为1。

请让我知道是否可以进一步澄清。谢谢!这是我当前运行的代码,但是“#Replicates”下面的所有内容都无法实现,因为我无法弄清楚:

# IRMs
IRMs = CorrectedDF[CorrectedDF['SampleID'].str.match('IRM')]
print('Total numer of IRM samples in the run is: {}' .format(len(IRMs.index)))

# BLANKs 
searchfor = ['blk', 'Blank', 'BLK', 'blank']
BLANKs = CorrectedDF[CorrectedDF['SampleID'].str.contains('|'.join(searchfor))]
print('Total numer of BLANKs in the run is: {}' .format(len(BLANKs.index)))

# OREAS 239
searchfor2 = ['OREAS 239', 'oreas 239', 'Oreas 239']
OREAS_239 = CorrectedDF[CorrectedDF['SampleID'].str.contains('|'.join(searchfor2))]
print('Total numer of OREAS 239 Samples in the run is: {}' .format(len(OREAS_239.index)))

# Cal Standards 
searchfor3 = ['Standard', 'Au 15']
CalSTD = CorrectedDF[CorrectedDF['SampleID'].str.contains('|'.join(searchfor3))]
print('Total numer of Cal Standard Samples in the run is: {}' .format(len(CalSTD.index)))

# Prep samples
searchfor4 = ['Prep']
Prep = CorrectedDF[CorrectedDF['SampleID'].str.contains('|'.join(searchfor4))]
print('Total numer of Prep Samples in the run is: {}' .format(len(Prep.index)))

# Replicates
searchfor5 = ['ORIG', 'PREPDUP']
Replicates = CorrectedDF[CorrectedDF['SampleID'].str.contains('|'.join(searchfor5))]
print('Total numer of Replicate Samples in the run is: {}' .format(len(Replicates.index)))

print('Total numer of ALL Samples in the run is: {}' .format(len(CorrectedDF.index)))
ClientSamples = len(CorrectedDF.index) - (len(IRMs.index) + len(BLANKs.index)
                                          + len(OREAS_239.index) + len(CalSTD.index) 
                                          + len(Prep.index) + len(Replicates.index))
print('Total numer of Client-ONLY Samples in the run is: {}' .format(ClientSamples))
python pandas sorting counting
1个回答
0
投票
df['Label'].str.extract('(XXX123 \d+-\d+)').nunique()

您可以仅使用正则表达式提取所需的内容,然后使用nunique找出有多少个唯一值。

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