Python 统计数据帧内列表中的出现次数
我有一个包含文章列表的熊猫数据框架;出口、发布日期、链接等。此数据框中的一列是关键字列表。例如,在“关键字”列中,每个单元格都包含一个类似于[drop、right、State、laws]的列表 我的最终目标是计算每天每个单词出现的次数。我面临的挑战是将关键字从列表中分离出来,然后将它们与它们出现的日期进行匹配。。。假设这是最符合逻辑的第一步 目前,我在下面的代码中找到了一个解决方案,但是我对python还不熟悉,在思考这些问题时,我仍然以Excel的思维方式思考。下面的代码可以工作,但速度非常慢。有没有快速的方法可以做到这一点Python 统计数据帧内列表中的出现次数,python,pandas,count,frequency,Python,Pandas,Count,Frequency,我有一个包含文章列表的熊猫数据框架;出口、发布日期、链接等。此数据框中的一列是关键字列表。例如,在“关键字”列中,每个单元格都包含一个类似于[drop、right、State、laws]的列表 我的最终目标是计算每天每个单词出现的次数。我面临的挑战是将关键字从列表中分离出来,然后将它们与它们出现的日期进行匹配。。。假设这是最符合逻辑的第一步 目前,我在下面的代码中找到了一个解决方案,但是我对python还不熟悉,在思考这些问题时,我仍然以Excel的思维方式思考。下面的代码可以工作,但速度非常慢
# Create a list of the keywords for articles in the last 30 days to determine their quantity
keyword_list = stories_full_recent_df['Keywords'].tolist()
keyword_list = [item for sublist in keyword_list for item in sublist]
# Create a blank dataframe and new iterator to write the keyword appearances to
wordtrends_df = pd.DataFrame(columns=['Captured_Date', 'Brand' , 'Coverage' ,'Keyword'])
r = 0
print("Creating table on keywords: {:,}".format(len(keyword_list)))
print(time.strftime("%H:%M:%S"))
# Write the keywords out into their own rows with the dates and origins in which they occur
while r <= len(keyword_list):
for i in stories_full_recent_df.index:
words = stories_full_recent_df.loc[i]['Keywords']
for word in words:
wordtrends_df.loc[r] = [stories_full_recent_df.loc[i]['Captured_Date'], stories_full_recent_df.loc[i]['Brand'],
stories_full_recent_df.loc[i]['Coverage'], word]
r += 1
print(time.strftime("%H:%M:%S"))
print("Keyword compilation complete.")
目前这个列表中大约有100000个单词,我花了一个小时来浏览这个列表。我希望能想出一个更快的方法 我认为这样做可以达到预期的效果:
wordtrends_df = pd.melt(pd.concat((stories_full_recent_df[['Brand', 'Captured_Date', 'Coverage']],
stories_full_recent_df.Keywords.apply(pd.Series)),axis=1),
id_vars=['Brand','Captured_Date','Coverage'],value_name='Keyword')\
.drop(['variable'],axis=1).dropna(subset=['Keyword'])
下面是一个小例子的解释
考虑一个示例数据帧:
df = pd.DataFrame({'Brand': ['X', 'Y'],
'Captured_Date': ['2017-04-01', '2017-04-02'],
'Coverage': [10, 20],
'Keywords': [['a', 'b', 'c'], ['c', 'd']]})
# Brand Captured_Date Coverage Keywords
# 0 X 2017-04-01 10 [a, b, c]
# 1 Y 2017-04-02 20 [c, d]
您可以做的第一件事是展开“关键字”列,以便每个关键字都占据自己的列:
a = df.Keywords.apply(pd.Series)
# 0 1 2
# 0 a b c
# 1 c d NaN
将其与原始的无关键字df列连接:
b = pd.concat((df[['Captured_Date','Brand','Coverage']],a),axis=1)
# Captured_Date Brand Coverage 0 1 2
# 0 2017-04-01 X 10 a b c
# 1 2017-04-02 Y 20 c d NaN
融化最后一个结果以按关键字创建行:
c = pd.melt(b,id_vars=['Captured_Date','Brand','Coverage'],value_name='Keyword')
# Captured_Date Brand Coverage variable Keyword
# 0 2017-04-01 X 10 0 a
# 1 2017-04-02 Y 20 0 c
# 2 2017-04-01 X 10 1 b
# 3 2017-04-02 Y 20 1 d
# 4 2017-04-01 X 10 2 c
# 5 2017-04-02 Y 20 2 NaN
最后,删除无用的变量
列,并删除缺少关键字
的行:
d = c.drop(['variable'],axis=1).dropna(subset=['Keyword'])
# Captured_Date Brand Coverage Keyword
# 0 2017-04-01 X 10 a
# 1 2017-04-02 Y 20 c
# 2 2017-04-01 X 10 b
# 3 2017-04-02 Y 20 d
# 4 2017-04-01 X 10 c
现在,您可以按关键字和日期进行计数了。这非常有效,而且速度非常快。谢谢你的帮助。melt对这个新手来说是新的。
d = c.drop(['variable'],axis=1).dropna(subset=['Keyword'])
# Captured_Date Brand Coverage Keyword
# 0 2017-04-01 X 10 a
# 1 2017-04-02 Y 20 c
# 2 2017-04-01 X 10 b
# 3 2017-04-02 Y 20 d
# 4 2017-04-01 X 10 c