如何计算给定 RFMin 和 RFMax 之间的重复标签

我正在阅读以下具有三列和多行的 CSV 文件:


Notation    RFMin       RFMax 

 AA100     1000         3333  

 BB200      3300        4500

目前我的输出文件如下所示:


 Notation   RFRange          Label 

 AA100       1000            AG, IF

 AA100       1259            AG, IF

 AA100       1518            AG, TE, WW 

 AA100       1777            AG, TE, WW

 AA100       2037            Unknown

 AA100       2296            Unknown

 AA100       2555            MH, WE   

 AA100       2814            MH, WE

 AA100       3074            DT, MH, WE

 AA100       3333            DT, MH, WE

 BB200       3300            DT, MH, WE

 BB200       3433            DT, MH, WE

 BB200       3567            DT, MH, WE

 BB200       3700            DT, MH, WE

 BB200       3833            DT, MH, WE

 BB200       3967            DT, MH, WE

 BB200       4100            Unknown

 BB200       4233            Unknown

 BB200       4366            Unknown

 BB200       4500            Unknown

  1. 我在使用 linspaceRFMIn之间打印 10 个数字RFMax

  2. 我正在打印 Notations Based on the Nnumbers of samples betweenRFMinRFMax,

  3. 我根据存在的条件标记#1 中的那 10 个数字

#4 我该怎么办?

  1. RFmin我想知道每个标签在每个和之间重复了多少次RFMax。例如,在1000-3333总共'AG'重复 4 次、'MH'重复 5 次、'IF'重复 2 次、'WW'2 次等之间...,在3300-4500-中'AG'重复 0 次、'MH'重复 6 次、 'IF'重复 0 次等...

这是代码:

import pandas as pd

import numpy as np


df = csv.read_csv(filePath)

N = 10

RFarray = []

Notation=[]

c = np.zeros((df.shape[0], N))


for index, col in df.iterrows():

    RFMin = col['RFMin']

    RFMax = col['RFMax']

    c[col] = np.linspace(RFMin, RFMax, N)


for ir, r1 in enumerate(c):

   for b in r1:

       RFarray.append(b)    

       Notation.append(df.loc[ir, 'Notation'])


dict = {'Notation': Notation,'RFRange': RFarray}



呼啦一阵风
浏览 106回答 1
1回答

千巷猫影

试试这个,合并 2 个数据帧,将标签转换为列表,添加范围过滤器,groupby 符号并将所有标签连接在一起,每个符号为 1 个列表,然后使用Counterfromcollections对列表中的每个元素进行计数:from collections import Counterdf2['Label'] = df2['Label'].str.split(',')df = pd.merge(df1, df2, on=['Notation'])df = df[(df['RFRange']>df['RFMin']) & (df['RFRange']<df['RFMax'])]df = df.groupby(by='Notation', as_index=False).agg({'Label': 'sum'})df['counts'] = df['Label'].apply(lambda x: Counter(x))print(df)&nbsp; Notation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;counts0&nbsp; &nbsp; AA100&nbsp; [AG, IF, AG, TE, WW, AG, TE, WW, Unknown, Unkn...&nbsp; {'AG': 3, 'IF': 1, 'TE': 2, 'WW': 2, 'Unknown'...1&nbsp; &nbsp; BB200&nbsp; [DT, MH, WE, DT, MH, WE, DT, MH, WE, DT, MH, W...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {'DT': 5, 'MH': 5, 'WE': 5, 'Unknown': 3}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python