Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 无法使用Seaborn绘图_Python_Pandas_Seaborn_Scatter Plot_Pandas Groupby - Fatal编程技术网

Python 无法使用Seaborn绘图

Python 无法使用Seaborn绘图,python,pandas,seaborn,scatter-plot,pandas-groupby,Python,Pandas,Seaborn,Scatter Plot,Pandas Groupby,你好,我的数据集如下 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 switch

你好,我的数据集如下

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=时间,y=计数 请帮助我,我如何才能绘制这个专栏


`

您可以尝试以下操作以
数据帧本身的形式获取数据

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'])

这是输出,因为您可以看到时间在重复,因此没有正确分组。您不认为groupby语句之后,必须只有一个时间计数项,例如,两行在abcd 07:53:15 3.0上都应分组为一行非常好的答案,谢谢,但在绘图时,它给了我一个错误,即float()参数必须是字符串或数字,不是“datetime.time”我可以尝试将datetime转换为浮动,但我想问,解决这个问题的最佳选择是什么?谢谢!您可以尝试ply.plot\u date()。我认为决定最佳方法还取决于其他因素。