作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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 |
facebook_dataframe.groupby(["Name", "Date", "Reactions"], as_index=False)["Engagement"].sum()
最佳答案
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
关于python - 如何从 Pandas MultiIndex 制作 Seaborn 线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62083824/
我是一名优秀的程序员,十分优秀!