python - 如何从 Pandas MultiIndex 制作 Seaborn 线图?

标签 python pandas seaborn

Noobish Seaborn/Pandas 问题:我有一个 Facebook 页面帖子数据的电子表格,看起来像这样(前 20 行):

| Name                   | ID              | Type      | Date             | Shares | Comments | Reactions | Engagement |
|------------------------|-----------------|-----------|------------------|--------|----------|-----------|------------|
| Herman Toothrot's Page | 201295459914847 | link      | 13/05/2020 09:00 | 61     | 39       | 610       | 710        |
| Guybrush's Page        | 167959249906191 | link      | 13/05/2020 09:04 | 4      | 27       | 481       | 512        |
| Elaine's Page          | 187202271820522 | album     | 13/05/2020 09:12 | 0      | 3        | 96        | 99         |
| Elaine's Page          | 187202271820522 | album     | 13/05/2020 09:14 | 1      | 14       | 426       | 441        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:20 | 3      | 8        | 158       | 169        |
| Herman Toothrot's Page | 201295459914847 | link      | 13/05/2020 09:20 | 26     | 101      | 508       | 635        |
| Elaine's Page          | 187202271820522 | undefined | 13/05/2020 09:23 | 1      | 11       | 109       | 121        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:28 | 2      | 257      | 987       | 1246       |
| Herman Toothrot's Page | 201295459914847 | photo     | 13/05/2020 09:30 | 1      | 0        | 178       | 179        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:31 | 3      | 6        | 162       | 171        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:38 | 44     | 143      | 4294      | 4481       |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:39 | 7      | 79       | 631       | 795        |
| Herman Toothrot's Page | 201295459914847 | link      | 13/05/2020 09:40 | 3      | 0        | 104       | 107        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:45 | 0      | 3        | 76        | 79         |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:45 | 20     | 78       | 1455      | 1553       |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:46 | 7      | 321      | 1847      | 2175       |
| Guybrush's Page        | 167959249906191 | link      | 13/05/2020 09:46 | 4      | 2        | 311       | 317        |
| Elaine's Page          | 187202271820522 | photo     | 13/05/2020 09:50 | 2      | 29       | 777       | 808        |
| Elaine's Page          | 187202271820522 | link      | 13/05/2020 09:53 | 0      | 0        | 115       | 115        |
| Herman Toothrot's Page | 201295459914847 | link      | 13/05/2020 10:00 | 143    | 255      | 10211     | 10609      |

实际数据集扩展了多天。

我想制作一个 Seaborn 线图,以时间为 X 轴(“日期”),然后将 COUNT 个帖子(按天)作为 Y 轴。

然后我想将数字变量之一设置为 SIZE 参数,并按 HUE 划分页面(“名称”)。

所以最终结果看起来像这个 Seaborn 教程示例:

enter image description here

我从概念上知道这需要按天重新采样(或某种分组?),以便按天为每个页面创建汇总数据桶,就像在这个 Excel 数据透视表中一样:

enter image description here

我怀疑这可能还需要 MultiIndex?

我以为我要去某个地方,
facebook_dataframe.groupby(["Name", "Date", "Reactions"], as_index=False)["Engagement"].sum()

...但我不想仅通过参与度(或仅通过任何一个变量)进行求和,而且我希望能够按天绘制。

我曾尝试按天对数据框进行重新采样,但我最终得到了一个显示计数或总和的系列,而我想要几天的时间段,所有数字变量都完好无损(根据上面的数据透视表)。

我希望这很清楚。我很欣赏提供的 20 个样本行都在一天之内,但是任何建议的解决方案都可以按分钟重新采样,方法是一样的吗?非常感谢任何帮助👍

最佳答案

IIUC,你要做的是groupby使用 pd.Grouper 给出你想要的频率和名称,然后使用 agg使用您想要获得的所有列和函数:

data = (df.groupby([pd.Grouper(key='Date', freq='5T'), #replace 5T by D to get daily agggregation
                    'Name'])
          .agg(count=('Name','count'), 
               sum_shares=('Shares','sum'), 
               sum_comments=('Comments','sum'), 
               sum_engagement=('Engagement','sum'), )
          .reset_index()
       )

print (data.head())
                 Date                      Name  count  sum_shares  \
0 2020-05-13 09:00:00   Guybrush's Page              1           4   
1 2020-05-13 09:00:00   Herman Toothrot's Page       1          61   
2 2020-05-13 09:10:00   Elaine's Page                2           1   
3 2020-05-13 09:20:00   Elaine's Page                2           4   
4 2020-05-13 09:20:00   Herman Toothrot's Page       1          26   

   sum_comments  sum_engagement  
0            27             512  
1            39             710  
2            17             540  
3            19             290  
4           101             635  

那么你可以使用 seaborn喜欢:
import seaborn as sns
sns.lineplot(x='Date', y='count', data=data,
             hue='Name', 
             size='sum_shares') #here for the size use what summed column you want

我不发布图片,因为给定的 20 行数据没有什么可看的。

关于python - 如何从 Pandas MultiIndex 制作 Seaborn 线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62083824/

相关文章:

python - 使用 Python 将字符串列合并到一组列表

python Pandas : return indexes of common rows

python - Seaborn 联合图,绝对轴标签未偏移

python - Python 中 matplotlib 和 seaborn 之间的绘图不一致

python - PySpark 窗口函数 : multiple conditions in orderBy on rangeBetween/rowsBetween

python - 不应在 for 循环中出现值错误?

json - 从包含 JSON 的 CSV 文件创建 Pandas DataFrame

python - Seaborn Jointplot 更改 Figsize

python - Django 自定义后端导致登录错误

java - Python 编程与 Java 编程