我有一堆關鍵字存盤在如下所示的 620x2 熊貓資料框中。我認為我需要將每個條目視為自己的集合,其中分號分隔元素。所以,我們最終得到了 1240 套。然后我希望能夠搜索我選擇的關鍵字一起出現的次數。例如,我想弄清楚有多少次“計算理論”和“關鍵基礎設施”作為這些集合中的一個子集以任意順序出現。有什么簡單的方法可以做到這一點嗎?

uj5u.com熱心網友回復:
用于.loc查找關鍵字是否一起出現。
將資料拆分為 1240 組后執行此操作。我不明白您是要創建新列還是只想保持列原樣。
# create a filter for keyword 1
filter_keyword_2 = (df['column_name'].str.contains('critical infrastructure'))
# create a filter for keyword 2
filter_keyword_2 = (df['column_name'].str.contains('computation theory'))
# you can create more filters with the same construction as above.
# To check the number of times both the keywords appear
len(df.loc[filter_keyword_1 & filter_keyword_2])
# To see the dataframe
subset_df = df.loc[filter_keyword_1 & filter_keyword_2]
.loc選擇條件資料框。subset_df=df[df['column_name'].str.contains('string')]如果您只有一個條件,您可以使用。
進行列拆分或任何其他處理之前,filters或在處理后再次運行過濾器。
uj5u.com熱心網友回復:
不確定這是否被認為是簡單的,但它確實有效。keyword_list是您要搜索的配對關鍵字串列。
df['Author Keywords'] = df['Author Keywords'].fillna('').str.split(';\s*').apply(set)
df['Index Keywords'] = df['Index Keywords'].fillna('').str.split(';\s*').apply(set)
df.apply(lambda x : x.apply(lambda y : all([kw in y for kw in keyword_list]))).sum().sum()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525789.html
