Python Pandas在特定索引上合并多个excel工作表

Python Pandas在特定索引上合并多个excel工作表,python,excel,pandas,Python,Excel,Pandas,我有一个包含多个工作表的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 dataframe,如

我有一个包含多个工作表的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 dataframe,如何以最佳方式导入此数据(按时间和项目代码组织)。 例如,我需要这个数据框,以便最终能够绘制商品代码5001的价格和库存变化图

谢谢你的帮助。我对python/pandas还是新手。 谢谢


我的解决方案。。。 这是我找到的解决问题的办法

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()
这将很好地生成具有分层列的数据帧

现在,我的新问题是,如何用这个数据帧绘制每种代码的价格变化? 我想看到5行(5001500250035045005),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:price v inventory的绘图:

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


我喜欢这个解决方案,你能看到我对这个原始问题所做的编辑,并帮助我绘制新数据框的图形吗?我认为最好开始一个新问题,因为我们已经从合并excel表格转移到与matplotlib中的多系列绘图更相关的问题。我已经准备好了一个示例实现。