[英]Reading multiple excel files from a certain path (with certain sheets names) into a single pandas data frame
How can I read only sheets '2g','3g','4g' from all the excel files exists in a certain path and write them to three different data frames.我如何只读取存在于特定路径中的所有 excel 文件中的工作表 '2g'、'3g'、'4g' 并将它们写入三个不同的数据帧。 All 2g sheets should be stored in a pandas data frame called (2g) All 3g sheets should be stored in a pandas data frame called (3g) All 4g sheets should be stored in a pandas data frame called (4g) following the screenshot of the excel files:
所有 2g 工作表应存储在名为 (2g) 的 pandas 数据框中 所有 3g 工作表应存储在名为 (3g) 的 pandas 数据框中 所有 4g 工作表应存储在名为 (4g) 的 pandas 数据框中excel 个文件:
and the sheets I want to read from each excel files as in the screen below:以及我想从每个 excel 文件中读取的工作表,如下面的屏幕所示:
You can use read_excel
with multiple sheet names:您可以将
read_excel
与多个工作表名称一起使用:
import pandas as pd
data2g = []
data3g = []
data4g = []
for xlsx in pathlib.Path('mobile').glob('*.xlsx'):
df2g, df3g, df4g = pd.read_excel(xlsx, sheet_name=['2G', '3G', '4G']).values()
data2g.append(df2g)
data3g.append(df3g)
data4g.append(df4g)
df2g = pd.concat(data2g, ignore_index=True)
df3g = pd.concat(data3g, ignore_index=True)
df4g = pd.concat(data4g, ignore_index=True)
As you can see, you can improve readability using loops:如您所见,您可以使用循环提高可读性:
import pandas as pd
from collections import defaultdict
data = defaultdict(list)
for xlsx in pathlib.Path('mobile').glob('*.xlsx'):
out = pd.read_excel(xlsx, sheet_name=['2G', '3G', '4G'])
for k, v in out.items():
data[k].append(v)
df2g, df3g, df4g = [pd.concat(data[k], ignore_index=True) for k in data]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.