Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 计算数据帧中字符串的出现次数_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 计算数据帧中字符串的出现次数

Python 3.x 计算数据帧中字符串的出现次数,python-3.x,pandas,Python 3.x,Pandas,我有一个dataframe,它有几个列,包括一个相关性(rel)列和一个cpc(cpc)列。rel越高,cpc中的值越相关。我已经编写了计算cpc列中每个值出现次数的代码,但我想做的是将每个cpc字符串乘以rel,这样我就可以将更相关的cpc的权重高于不相关的cpc。例如,在第一行中,rel是74,因此每个字符串H01L51/5036、H01L51/006和H01L51/5016将被计数74次,而不是一次 我用来计数的代码是: from collections import Counter fl

我有一个dataframe,它有几个列,包括一个相关性(rel)列和一个cpc(cpc)列。rel越高,cpc中的值越相关。我已经编写了计算cpc列中每个值出现次数的代码,但我想做的是将每个cpc字符串乘以
rel
,这样我就可以将更相关的cpc的权重高于不相关的cpc。例如,在第一行中,
rel
是74,因此每个字符串
H01L51/5036
H01L51/006
H01L51/5016
将被计数74次,而不是一次

我用来计数的代码是:

from collections import Counter
flat_cpcSet = [item for sublist in cpcSet for item in sublist]
result = Counter(flat_cpcSet)
cpcSet是一个列表列表。此后,我将cpc列表添加到数据帧中,而不是单独的列表

数据帧如下所示:

>df
    appID   rel au  x-num   cpc
0   12552285    74  1719    66561   ['H01L51/5036', 'H01L51/006', 'H01L51/5016']
1   11266356    57  2621    89783   ['C22B7/006', 'B01B1/005', 'C22B3/02', 'C22B3/065', 'C22B7/007', 'C22B11/042', 'C22B11/048', 'C22B59/00', 'Y02P10/214', 'Y02P10/234']
2   14273884    55  2864    69308   ['A46B9/021']
3   12524394    50  2459    60344   ['F02B37/013', 'F01D17/105', 'F01D25/24', 'F01N13/10', 'F02B37/02', 'F02B37/183', 'F02C6/12', 'F02B37/004', 'F02M26/16', 'F05D2270/58', 'Y02T50/671', 'Y02T10/144', 'F05D2230/21']
4   12023698    39  1757    68832   ['F01K23/101', 'Y02E20/16']
5   12421790    36  1635    68488   ['G09G3/3685', 'G09G3/3611', 'G09G3/20', 'G09G2330/021', 'G09G2330/06', 'G09G2370/08']
6   13177981    24  1631    83216   ['C07D209/88', 'A61K31/403', 'C07D209/82', 'A61K31/404', 'A61K31/4045', 'A61K31/437', 'A61K31/4439', 'A61K31/506', 'C07D209/08', 'C07D209/86', 'C07D401/06', 'C07D401/12', 'C07D403/06', 'C07D403/12', 'C07D405/12', 'C07D413/06', 'C07D471/04', 'C07D495/04', 'C07F5/022', 'A61K31/4155', 'A61K31/4188', 'A61K31/4192', 'A61K31/422']
7   13065610    23  2428    71350   ['G06Q50/24', 'G06F19/00']
8   13756098    17  2484    61743   ['F28D20/025', 'F28D20/02', 'F28D20/026', 'F28F2245/06', 'F28F2265/12', 'Y02E60/145', 'F28F2265/14']
9   12823912    6   2865    61269   []
我想要的是一个新的数据帧,它看起来像(注意,只是一个示例格式,不适合上述数据):

我一直试图写一些东西,大致如下:

x = 0
while x <= len(df['cpc']):
    y = 0
    while y <= len(df['cpc'][x]):
        # code to multiply the string df['cpc'][x] by the int df['rel'][0]
        y += 1
    x += 1
    # code to count the occurrence of the strings and write a new dataframe
x=0

而x你几乎拥有你所需要的一切。只需调整您的
cpc
列并使用其上方的计数器即可:

df['w_cpc'] =df.cpc*df.rel
flat_data = list(x for l in df.w_cpc for x in l)
d = Counter(flat_data)
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df['w_cpc'] =df.cpc*df.rel
flat_data = list(x for l in df.w_cpc for x in l)
d = Counter(flat_data)
df = pd.DataFrame.from_dict(d, orient='index').reset_index()