具有多个索引的Pandas线图子图

2024-04-29 16:25:27 发布

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

我想创建线图子批次,每个客户的每个代码在一个单独的文件中为每个客户拥有自己的子批次。在

          Date    Code  Customer     Purchases
1    6/22/2016   6-BZt     Piggy             8
2    6/22/2016   7rTPn     Piggy             1
3    6/22/2016   Hb4vZ     Piggy             1
4    6/22/2016   L0xs5     Piggy            94
5    6/22/2016   S5cLN     Goose             2
6    6/22/2016   k4Yp5     Goose             1
8    6/21/2016   6-BZt     Goose             8
9    6/21/2016   7rTPn     Piggy             1
10   6/21/2016   Hb4vZ     Piggy             1
11   6/21/2016   L0xs5     Piggy            94
12   6/21/2016   S5cLN     Goose             2
13   6/21/2016   k4Yp5     Goose             1

我试过了

^{pr2}$

但它没有按我想要的方式输出。在


Tags: 文件代码date客户codecustomer线图purchases
1条回答
网友
1楼 · 发布于 2024-04-29 16:25:27

因此,我认为您需要执行的操作顺序是首先将数据帧拆分为每个客户的单独数据帧,设置索引,然后取消堆叠和绘图。给定任意数量的客户,您可以这样做

lineSess.sort_values(by='Customer', inplace=True)
lineSess.set_index('Customer', inplace=True)

# get list of unique customer names
customers = lineSess.index.unique().tolist()

# create an empty dataframe for each customer name
customer_dfs = {k: v for k, v in zip(customers, [pd.DataFrame()]*len(customers))}

# fill the empty dataframes with the data corresponding to each particular customer
for key, df in customer_dfs.iteritems(): # customer_dfs.items() for python-3.x
    df = lineSess.loc[lineSess.index==key]
    df = df.set_index(['Date', 'Code'], drop=True)
    df['Purchases'].unstack(level='Code').plot(subplots=True, title=key)

根据你提供的数据,这些图看起来相当枯燥,因为每天的购买量没有变化。但是,假设这只是数据集的一部分,那么这些图可能会提供更多信息。在

相关问题 更多 >