python - 无法使用 Seaborn 绘图
问题描述
您好我的数据集如下
username switch_state time
abcd sw-off 07:53:15 +05:00
abcd sw-on 07:53:15 +05:00
现在使用这个我需要找到在给定的一天一天中有多少次开关状态被操纵,即打开或关闭。我的测试代码如下
switch_off=df.loc[df['switch_state']=='sw-off']#only off switches
groupy_result=switch_off.groupby(['time','username']).count()['switch_state'].unstack#grouping the data on the base of time and username and finding the count on a given day. fair enough
该 groupby 子句的结果为
print(groupy_result)
username abcd
time
05:08:35 3
07:53:15 3
07:58:40 1
现在您可以看到计数在时间列中连接。我需要将它们分开,以便我可以使用 Seaborn 散点图绘制它。我需要有 x 和 y 值,在我的情况下将是 x=time,y=count 请帮助我如何绘制此列。
`
解决方案
您可以尝试以下方法来获取数据DataFrame
本身
df = df.loc[df['switch_state']=='sw-off']
df['count'] = df.groupby(['username','time'])['username'].transform('count')
这两行代码将为您提供一个更新的数据框df
,其中将添加一个名为count
.
df = df.drop_duplicates(subset=['username', 'time'], keep='first')
上面的行将删除重复的行。然后你可以绘制df['time']
和df['count']
。
plt.scatter(df['time'], df['count'])
推荐阅读
- python - Scrapy:如何同时运行多个顺序爬取?
- javascript - 变异复合 zustand 对象状态
- java - Javamail:IMAP:邮件的内容类型
- quote - 采购平台:报价请求在过期前保持打开状态多长时间?
- python - django 和 postgres 什么是 uuid 的替代品
- python - 如何删除 matplotlib 图像的框架并将背景颜色设置为透明
- python - Kivy exe 文件显示一个空白的灰色画布
- python - 如何使用从 python 文件到其他 python 文件的数据路径
- kubernetes - 如何在 GKE 上禁用 Kube-DNS
- java - maven 无法收集依赖项 spark commons-code