Pandas将Excel数据按列和图表散点图进行平均分组

2024-10-01 13:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我从几个Excel文件中读取了一组数据。我可以很容易地阅读,合并和分组的数据与熊猫。我对数据有两列感兴趣的“产品类型”和“测试持续时间”

包含从Excel文件读取的数据的数据帧称为oData。在

oDataGroupedByProductType = oData.groupby(['Product Type'])

我用plotly绘制了一个图,如下所示,但是plotly不将数据保密,如果我想让数据私有化,我必须付费。付款不是一种选择。 enter image description here 如何使用pandas和/或matplotlib制作相同的图形,但也要显示每个产品类型的平均值?在


Tags: 文件数据类型产品type绘制plotlyproduct
3条回答

正如Bound所说,您可以用stripplot(seaborn文档页面的示例)几行来完成。在

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips") # load some sample data
ax = sns.stripplot(x="day", y="total_bill", data=tips)

enter image description here

假设您有一些数据帧:

In [4]: df.head(20)
Out[4]:
   product      value
0        c   5.155740
1        c   8.983128
2        c   5.150390
3        a   8.379866
4        c   8.094536
5        c   7.464706
6        b   3.690430
7        a   5.547448
8        a   7.709569
9        c   8.398026
10       a   7.317957
11       b   7.821332
12       b   8.815495
13       c   6.646533
14       c   8.239603
15       c   7.585408
16       a   7.946760
17       c   5.276864
18       c   8.793054
19       b  11.573413

您需要有一个数字值,以便产品绘制它,所以快速而干练,只需通过映射数值来创建一个新列:

^{pr2}$

当然,有很多方法可以达到这个目的。。。在

现在,创建一个新列:

In [8]: df['product_code'] = df['product'].map(product_map)

In [9]: df.head(20)
Out[9]:
   product      value  product_code
0        c   5.155740             1
1        c   8.983128             1
2        c   5.150390             1
3        a   8.379866             2
4        c   8.094536             1
5        c   7.464706             1
6        b   3.690430             3
7        a   5.547448             2
8        a   7.709569             2
9        c   8.398026             1
10       a   7.317957             2
11       b   7.821332             3
12       b   8.815495             3
13       c   6.646533             1
14       c   8.239603             1
15       c   7.585408             1
16       a   7.946760             2
17       c   5.276864             1
18       c   8.793054             1
19       b  11.573413             3

现在,在pandas中使用plot助手方法,它基本上是matplotlib的包装:

In [10]: df.plot(kind='scatter', x = 'product_code', y = 'value')
Out[10]: <matplotlib.axes._subplots.AxesSubplot at 0x12235abe0>

以及输出:

enter image description here

很明显,这是快速和肮脏的,但它应该让你的方式。。。在

如果其他人有一个非常类似的问题,并希望看到最终结果,我最终使用了seaborn,如下所示:

import seaborn as sns
import matplotlib.pyplot as plt
...
sns.set_style("whitegrid")
sns.boxplot(x=oData['Product Type'],
          y=oData['Test Duration?'],
          data=oData)
plt.savefig('Test Duration vs. Product Type.png')

图表如下所示。出于隐私的原因,我已经模糊了图表上的产品标签。在

enter image description here

相关问题 更多 >