Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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,我有一个带有一些tweet的数据帧,类似这样: tweets = pd.Series(['This is a tweet example #help #thankyou', 'Second tweet example #help', 'Third tweet example #help #stackoverflow']) tweets_df = pd.DataFrame({'Tweets': tweets})

我有一个带有一些tweet的数据帧,类似这样:

tweets = pd.Series(['This is a tweet example #help #thankyou', 
                    'Second tweet example #help', 
                    'Third tweet example #help #stackoverflow'])

tweets_df = pd.DataFrame({'Tweets': tweets})
然后,我将hashtags放在dataframe的另一列中

tweets_df['hashtags'] = tweets_df['Tweets'].apply(lambda twt : re.findall(r"#(\w+)", twt))
现在我想计算它们,并将结果放入另一个数据帧中。我尝试了以下方法,但没有成功

tweets_df['hashtags'].str.split(expand=True).stack().value_counts()
结果必须类似于:

#help           2
#thankyou       1
#stackoverflow  1
让我们使用extractall和value_计数:

输出:

#help             3
#stackoverflow    1
#thankyou         1
Name: 0, dtype: int64
让我们使用extractall和value_计数:

输出:

#help             3
#stackoverflow    1
#thankyou         1
Name: 0, dtype: int64
你可以用柜台

你可以用柜台

您不需要将tweet制作成数据帧。只需从那里执行提取:

tweets.str.extractall(r'(\#\w*)')[0].value_counts()

#help             3
#stackoverflow    1
#thankyou         1
Name: 0, dtype: int64
您不需要将tweet制作成数据帧。只需从那里执行提取:

tweets.str.extractall(r'(\#\w*)')[0].value_counts()

#help             3
#stackoverflow    1
#thankyou         1
Name: 0, dtype: int64
帮不上忙吗?帮不上忙吗?