Python 具有多个索引的熊猫线图子图
我想创建线图子图,每个客户的每个代码都在一个单独的文件中为每个客户创建自己的子图Python 具有多个索引的熊猫线图子图,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我想创建线图子图,每个客户的每个代码都在一个单独的文件中为每个客户创建自己的子图 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 L0xs
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
我试过了
lineSess = lineSess.set_index(['Date', 'Customer', 'Code'])
lineSess.unstack().plot(subplots=True)
但是它没有按我想要的方式输出。因此我认为您需要遵循的操作顺序是,首先为每个客户将数据帧拆分为单独的数据帧,设置索引,然后取消堆栈并打印。给定任意数量的客户,您可以这样做
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)
这些曲线图与您提供的数据相比会显得相当枯燥,因为购买的数量从一天到下一天都没有变化。但假设这只是数据集的一部分,绘图可能会提供更多信息。因此,我认为您需要遵循的操作顺序是,首先为每个客户将数据帧拆分为单独的数据帧,设置索引,然后取消堆栈并进行绘图。给定任意数量的客户,您可以这样做
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)
这些曲线图与您提供的数据相比会显得相当枯燥,因为购买的数量从一天到下一天都没有变化。但是假设这只是数据集的一部分,那么这些图可能会提供更多的信息。几个问题:(1)单独的文件是指单独的数字吗?(2) 你是说你想要2个数字,每个数字有6个子批次,因为这是唯一代码的数量?(3) 我想你希望购买的数量是y轴,但x轴应该是什么?1)是的,我是说数字。哎呀。2) 没错。3) X轴是日期范围几个问题:(1)单独的文件是指单独的数字吗?(2) 你是说你想要2个数字,每个数字有6个子批次,因为这是唯一代码的数量?(3) 我想你希望购买的数量是y轴,但x轴应该是什么?1)是的,我是说数字。哎呀。2) 没错。3) X轴是日期范围