Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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 分割一只熊猫';findall';将结果列表分为多个项目,按唯一性分组_Python_Pandas - Fatal编程技术网

Python 分割一只熊猫';findall';将结果列表分为多个项目,按唯一性分组

Python 分割一只熊猫';findall';将结果列表分为多个项目,按唯一性分组,python,pandas,Python,Pandas,我已经下载了我的Twitter档案,我正试图对我与谁交谈最多进行一些分析 Tweets CSV列如下所示: tweet_id,in_reply_to_status_id,in_reply_to_user_id,retweeted_status_id,retweeted_status_user_id,timestamp,source 我使用read_csv()将tweets.csv文件导入到名为“indata”的数据框中 然后,为了获得tweets中提到的所有@handles的列表,我使用了以下

我已经下载了我的Twitter档案,我正试图对我与谁交谈最多进行一些分析

Tweets CSV列如下所示:

tweet_id,in_reply_to_status_id,in_reply_to_user_id,retweeted_status_id,retweeted_status_user_id,timestamp,source
我使用read_csv()将tweets.csv文件导入到名为“indata”的数据框中

然后,为了获得tweets中提到的所有@handles的列表,我使用了以下方法:

handles = indata['text'].str.findall('@[a-zA-Z0-9_-]*')
结果:

timestamp
...
2013-04-12 11:24:27                                [@danbarker]
2013-04-12 11:22:32                                  [@SeekTom]
2013-04-12 10:50:45    [@33Digital, @HotwirePR, @kobygeddes, @]
2013-04-12 08:00:03                              [@mccandelish]
2013-04-12 07:59:01                                [@Mumbrella]
...
Name: text, dtype: object
我想能够做的是根据每个手柄和日期进行分组,以显示这些年来我与谁交谈最多的人数


有什么建议吗?

一种简单的方法可能是应用Series构造函数将其放入一个数据帧并堆叠到一个系列中(这样您就可以使用value\u counts)。。。如果不关心索引/时间戳,可以使用集合(可能更快):

注意:我在这里使用
+
而不是
*
,因为我认为不应该包括
@
本身

使用此功能非常简单:

In [21]: from collections import Counter

In [22]: Counter(at_mentions.sum())
Out[22]: Counter({'@a': 2, '@b': 1, '@c': 1})
熊猫方式将保留索引(时间)信息

用于获取数据帧并将其转换为系列的系列构造函数:

In [31]: all_mentions = at_mentions.apply(pd.Series)

In [32]: all_mentions
Out[33]:
    0    1
0  @a   @b
1  @a  NaN
2  @c  NaN
我们可以在这里整理名称,以便更详细地描述正在发生的事情:

In [33]: all_mentions.columns.name = 'at_number'

In [34]: all_mentions.index.name = 'tweet'  # this is timestamp in your example
现在,当我们堆叠时,我们会看到级别的名称:

In [35]: all_mentions = all_mentions.stack()

In [36]: all_mentions
Out[36]:
tweet  at_number
1      0            @a
       1            @b
2      0            @a
3      0            @c
dtype: object
我们可以在这里进行许多其他分析,例如:


最终的结果相当于pd.Series(计数器(at_indications.sum())

非常全面的答案,谢谢Andy。熊猫的原生方式对我来说很有效——我喜欢保留时间戳的想法,这样我就可以按日期分组——但是谢谢你介绍我收藏。柜台也是。
In [33]: all_mentions.columns.name = 'at_number'

In [34]: all_mentions.index.name = 'tweet'  # this is timestamp in your example
In [35]: all_mentions = all_mentions.stack()

In [36]: all_mentions
Out[36]:
tweet  at_number
1      0            @a
       1            @b
2      0            @a
3      0            @c
dtype: object
In [37]: all_mentions.value_counts()
Out[37]:
@a    2
@c    1
@b    1
dtype: int64