pandas结合了多个特定索引的Excel工作表

我有一个包含多个工作表的Excel文件。 每个工作表包含特定月份的单个项目代码的价格和库存数据。

例如…

sheetname = 201509

code price inventory 5001 5 92 5002 7 50 5003 6 65 

sheetname = 201508

 code price inventory 5001 8 60 5002 10 51 5003 6 61 

使用pandas数据框,导入这些数据的最好方法是按时间和项目代码进行组织。 例如,我需要这个数据框来最终能够对物品代码5001的价格和库存的变化进行图表化。

我将不胜感激您的帮助。 我还是新来的python/pandas。 谢谢。


我的解决scheme…这是我发现我的问题的解决scheme。

 import pandas as pd import numpy as np import matplotlib.pyplot as plt D201509 = pd.read_excel('ExampleSpreadsheet.xlsx', sheetname='201509', index_col='Code') D201508 = pd.read_excel('ExampleSpreadsheet.xlsx', sheetname='201508', index_col='Code') D201507 = pd.read_excel('ExampleSpreadsheet.xlsx', sheetname='201507', index_col='Code') D201506 = pd.read_excel('ExampleSpreadsheet.xlsx', sheetname='201506', index_col='Code') D201505 = pd.read_excel('ExampleSpreadsheet.xlsx', sheetname='201505', index_col='Code') total = pd.concat(dict(D201509=D201509, D201508=D201508, D201507=D201507, D201506=D201506, D201505=D201505), axis=1) total.head() 

这将很好地产生与分层列这个dataframe..

在这里输入图像说明

现在我的新问题是如何绘制每个代码的价格变化与这个数据框? 我想看5行(5001,5002,5003,5004,5005),其中x轴是时间(D201505,D201506等),y轴是价格值。

谢谢。

这将把你的数据放到一个数据框中,并在5001上做散点图

 import pandas as pd import matplotlib.pyplot as plt import xlrd file = r'C:\dickster\data.xlsx' list_dfs = [] xls = xlrd.open_workbook(file, on_demand=True) for sheet_name in xls.sheet_names(): df = pd.read_excel(file,sheet_name) df['time'] = sheet_name list_dfs.append(df) dfs = pd.concat(list_dfs,axis=0) dfs = dfs.sort(['time','code']) 

这看起来像:

  code price inventory time 0 5001 8 60 201508 1 5002 10 51 201508 2 5003 6 61 201508 0 5001 5 92 201509 1 5002 7 50 201509 2 5003 6 65 201509 

而现在的情节5001:价格v盘点:

 dfs[dfs['code']==5001].plot(x='price',y='inventory',kind='scatter') plt.show() 

这产生:

在这里输入图像说明