从多索引dataframe绘制的Pandas Limeseries

发布于 2025-02-03 03:07:39 字数 1273 浏览 1 评论 0原文

我有一个多索引数据框架,看起来像:

                                          col1        col2        col3
timestamp        type        class
1653385980729    audio       dc           3           10          1
                             ec           5           7           2
                 video       dc           10          18          5
                             ec           7           14          4
1653385981729    audio       dc           4           8           4
                             ec           5           8           2
                 video       dc           9           15          3
                             ec           9           17          6
1653385982729    audio       dc           3           10          3
                             ec           6           8           2
                 video       dc           11          16          6
                             ec           9           18          6
.
.
.

现在我正在尝试从此数据框架的列中创建一些图形。不同列的每个图将看起来像这样(不介意随机值):

“示例图”

当我尝试通过分离列绘制并使用plot()时,它成为绝对的混乱。我应该如何操纵数据框以实现我想要的东西?

I have a multi index data frame which looks like:

                                          col1        col2        col3
timestamp        type        class
1653385980729    audio       dc           3           10          1
                             ec           5           7           2
                 video       dc           10          18          5
                             ec           7           14          4
1653385981729    audio       dc           4           8           4
                             ec           5           8           2
                 video       dc           9           15          3
                             ec           9           17          6
1653385982729    audio       dc           3           10          3
                             ec           6           8           2
                 video       dc           11          16          6
                             ec           9           18          6
.
.
.

Now I am trying to create some graphs out of columns of this dataframe. Each graph for different columns will look like this (don't mind the random values):

example graph

When I try to draw it by seperating columns and using plot() it becomes an absolute mess. How should I manipulate the dataframe to achieve what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2025-02-10 03:07:39

使用 通过Flatten 列中的第二和第三级

df1 = df['col1'].unstack([1,2])
df1.columns = [f'{a}/{b}' for a, b in df1.columns]
print (df1)
               audio/dc  audio/ec  video/dc  video/ec
timestamp                                            
1653385980729         3         5        10         7
1653385981729         4         5         9         9
1653385982729         3         6        11         9

df1.plot()

Use Series.unstack by second and third level with flatten MultiIndex in columns:

df1 = df['col1'].unstack([1,2])
df1.columns = [f'{a}/{b}' for a, b in df1.columns]
print (df1)
               audio/dc  audio/ec  video/dc  video/ec
timestamp                                            
1653385980729         3         5        10         7
1653385981729         4         5         9         9
1653385982729         3         6        11         9

df1.plot()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文