获得df列中的哪个元素出现在另一个df列中的每个单独元素(各种单独字符串的列表)上的频率最高]

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

我的pandas数据框中有一个叫做'tags'的列,它是多个字符串的列表。

[abc, 123, xyz]
[456, 123]
[abc, 123, xyz]

而且我还有另一种列技术,每个技术都有一个字符串

win
mac
win

[请让我知道是否有一种方法可以让我了解标记中每个元素在技术中出现频率最高的元素。例如,与其他技术相比,“ abc”最常与“ win”相关联。因此输出应如下所示:

abc win
123 win
xyz win
456 mac
python pandas dataframe associations
3个回答
2
投票

IIUC,您可以explode Tags列,然后将crosstabidxmax一起使用:

输入:

d = {'Tags':[['abc', 123, 'xyz'],[456, 123],['abc', 123, 'xyz']],
     'tech':['win','mac','win']}
df = pd.DataFrame(d)
print(df)

              Tags tech
0  [abc, 123, xyz]  win
1       [456, 123]  mac
2  [abc, 123, xyz]  win

解决方案:

m = df.explode('Tags')
out = pd.crosstab(m['Tags'],m['tech']).idxmax(1)


Tags
123    win
456    mac
abc    win
xyz    win
dtype: object

1
投票

您好,我建议以下内容:


import pandas as pd
# I reproduce your example
df = pd.DataFrame({"tags": [["abc", "123", "xyz"], ["456", "123"], ["abc", "123", "xyz"]],
                   "tech": ["win", "mac", "win"]})
# I use explode to have one row per tag
df = df.explode(column="tags")
# then I set index for tags
df = df.set_index("tags").sort_index()

# And then I take the most frequent value by defining a mode function
def mode(x):
    '''
    Returns mode 
    '''
    return x.value_counts().index[0]
res = df.groupby(level=0).agg(mode)

我知道

     tech
tags     
123   win
456   mac
abc   win
xyz   win

0
投票

如果您还希望与标签关联的频率:

import pandas as pd
from collections import Counter


df = pd.DataFrame({'tech':['win', 'mac', 'win'], 
              'tags':[['abc', 123, 'xyz'], [456, 123], ['abc', 234, 'xyz']]})

df = df.groupby('tech').sum() # concatenate by tech the lists

df['freq'] = [Counter(el) for el in df['tags']] # convert each list to a dict of frequency

final_df = pd.DataFrame()

# explode the column of dicts
for row in df.iterrows():
    tech = row[0]      # get the value in the metric column
    for key, value in row[1][1].items():
        tmp_df = pd.DataFrame({
            'tech':tech,
            'tag': key,
            'frequency': value
        }, index=[0])

        final_df = final_df.append(tmp_df) # append the tmp_df to our final df

final_df = final_df.reset_index(drop=True)  
© www.soinside.com 2019 - 2024. All rights reserved.