Python数据帧中的行排序/计数
我正在尝试开发一个质量检查脚本,它将遍历一组数据(在熊猫数据框架中),并计算不同类型样本的总数。以下是数据库中的一个示例: 有问题的样品都是有问题的Python数据帧中的行排序/计数,python,pandas,sorting,counting,Python,Pandas,Sorting,Counting,我正在尝试开发一个质量检查脚本,它将遍历一组数据(在熊猫数据框架中),并计算不同类型样本的总数。以下是数据库中的一个示例: 有问题的样品都是有问题的 XXX123 我的当前脚本选择和挑选所有的QC样本,如空白或IRM,但是我对实际的XXX123的计数有困难,因为其中一些获得了两种类型的副本,如内部质量检查。p> 一种是带有“ORIG”和“PREP”的类型 第二种是带“.1”和“.2”的 另外一个问题是,很少有一个示例同时获得这两个示例,正如您在XXX123 85-90中看到的那样 最后,问题
XXX123
我的当前脚本选择和挑选所有的QC样本,如空白或IRM,但是我对实际的XXX123的计数有困难,因为其中一些获得了两种类型的副本,如内部质量检查。p>
- 只要有一行包含“.1”和下面的一行 包含“.2”-将这两个条目计为1
- 只要有一行包含“ORIG”和下面的一行 包含“PREPDUP”-将这两个条目计为1
- 只要有一行包含“ORIG.1”和下面的一行 包含“原始2”和下面第三个,包含“PREPDUP”- 将这三个条目计为1
# 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))
您可以使用正则表达式提取您要查找的内容,然后使用nunique找出有多少个唯一值。请以文本格式提供
csv
。另外,变量和函数名应遵循小写字母加下划线的样式。谢谢。然而,我想我在查看数据集时忘记了另一件事。因此,这些工作基本上有三种类型的重复:1。以“.1”和“.2”结尾。在第三句末尾加上“ORIG”和“PREP”。但也有不应算作一的重复项,以“A”和“B”结尾。换句话说,这些不应该算作1,而应该算作2。示例“XXX123 A”和“XXX123 B”
df['Label'].str.extract('(XXX123 \d+-\d+)').nunique()