繁体   English   中英

创建多个直方图,按用户从 Python 中的大型单个数据集分组

[英]Create multiple histograms, grouped by user from a large, single dataset in Python

我有一个大数据集 df:

  User              duration

  amy                582         
  amy                27
  amy                592
  amy                16
  amy                250
  tom                33
  tom                10
  tom                40
  tom                100

我想按用户分组,然后为每个用户创建一个直方图:

 amy (histogram image)


 tom (histogram image)

这是dput:

structure(list(User = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L), .Label = c("amy", "tom"), class = "factor"), duration = c(582L, 
27L, 592L, 16L, 250L, 33L, 10L, 40L, 100L)), class = "data.frame", row.names = c(NA, 
-9L))

我知道如何使用以下代码在 Python 中创建直方图:,但是如何在 Python 中创建多个直方图,按用户分组。 我应该创建字典吗?

 import numpy as np
 import matplotlib.mlab as mlab
 import matplotlib.pyplot as plt

 df = (amy[582,27, 592, 16, 250], tom[33,10,40,100])
 num_bins = 20
 n, bins, patches = plt.hist(x, num_bins, facecolor='blue', alpha=0.5)
 plt.show()

任何建议表示赞赏。

df = pd.DataFrame({'user':['amy', 'amy','amy','amy','amy', 'tom', 'tom', 'tom','tom',],
              'duration': [582, 27, 592, 16, 250, 33, 10, 40, 100]})

ax = df['duration'].hist(by=df['user'])

for a in ax.flatten():
    a.set_xlabel("duration")
    a.set_ylabel("frequency")

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM