简体   繁体   English

正态分布根据pandas数据帧的名称绘制

[英]Normal Distribution Plot by name from pandas dataframe

I have a dataframe like below: 我有一个如下数据框:

dateTime        Name    DateTime        day seconds zscore
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 15:17 james   11/1/2016 15:17 Tue 55020   1.158266091
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:41 james   11/1/2016 13:41 Tue 49260   -0.836236954
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 13:42 james   11/1/2016 13:42 Tue 49320   -0.81546088
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:07  matt    11/1/2016 9:07  Tue 32820   -0.223746683
11/1/2016 9:08  matt    11/1/2016 9:08  Tue 32880   -0.111873342
11/1/2016 9:48  matt    11/1/2016 9:48  Tue 35280   4.363060322

zscore is calculated as below: zscore的计算方法如下:

grp2 = df.groupby(['Name'])['seconds']
df['zscore'] = grp2.transform(lambda x: (x - x.mean()) / x.std(ddof=1))

I would like to plot my data in a bell curve / normal distribution plot and save this as a picture/pdf file for each Name in my dataframe. 我想在钟形曲线/正态分布图中绘制我的数据,并将其保存为我的数据帧中每个Name的图片/ pdf文件。

I have tried to plot the zscores like below: 我试图绘制如下的zscores:

df['by_name'].plot(kind='hist', normed=True)
range = np.arange(-7, 7, 0.001)
plt.plot(range, norm.pdf(range,0,1))
plt.show()

How would I go about plotting the by_name zscores column for each name in my data? 我如何为数据中的每个名称绘制by_name zscores列?

np.random.seed([3,1415])
df = pd.DataFrame(dict(
        Name='matt joe adam farley'.split() * 100,
        Seconds=np.random.randint(4000, 5000, 400)
    ))

df['Zscore'] = df.groupby('Name').Seconds.apply(lambda x: x.div(x.mean()))

df.groupby('Name').Zscore.plot.kde()

在此输入图像描述


split out plots 拆分出的情节

g = df.groupby('Name').Zscore
n = g.ngroups
fig, axes = plt.subplots(n // 2, 2, figsize=(6, 6), sharex=True, sharey=True)
for i, (name, group) in enumerate(g):
    r, c = i // 2, i % 2
    group.plot.kde(title=name, ax=axes[r, c])
fig.tight_layout()

在此输入图像描述


kde + hist kde + hist

g = df.groupby('Name').Zscore
n = g.ngroups
fig, axes = plt.subplots(n // 2, 2, figsize=(6, 6), sharex=True, sharey=True)
for i, (name, group) in enumerate(g):
    r, c = i // 2, i % 2
    a1 = axes[r, c]
    a2 = a1.twinx()
    group.plot.hist(ax=a2, alpha=.3)
    group.plot.kde(title=name, ax=a1, c='r')
fig.tight_layout()

在此输入图像描述

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

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