如何使用python从excel路径列表创建特定excel信息的大型数据框

2024-09-29 06:26:08 发布

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

也许是个简单的办法。 我希望从目录中的许多相同样式的excel工作簿中提取特定信息,并将所有特定信息连接到一个工作簿中(同时更改格式)。我已经完成了这个任务的每一部分,除了成功地从不同的工作簿中创建一个由n列组成的大数据帧(与读取的xlsx文件的数量成比例)。每个已读工作簿只有一张表['Sheet1']。这听起来像是我采取了正确的方法吗?我目前正在使用for循环来收集这些数据。你知道吗

在网上进行了大量的研究(Github、youtube、stackoverflow),其他人说制作一个大数据帧,然后连接起来。我尝试使用for循环来创建这个数据帧;但是,我没有看到用户像我这样“拼凑”数据位来形成数据帧。我不认为这会妨碍手术。我意识到我没有附加或连接,只是不知道去哪里。你知道吗

for i in filepaths:           #filepaths is a list of n filepaths`
    df = pd.read_excel(i) #read the excel sheets`
    info = otherslices   #condensed form of added slices from df`
    Final = pd.DataFrame(info)  #expected big dataframe`

预期结果应该是相邻的列(分别来自每个excel表)

Excel1  Excel2    ->  Excel(n)
info1a  info1b
info2a  info2b
info3a  info3b
...     ...

我现在在循环中使用“print(Final)”时得到的是

Excel1
info1a
info2a
info3a
...
Excel2
info1b
info2b
info3b
...
|
Excel(n)

然而,我从这个循环中得到的数据帧(当我键入Final时)只是 最后一个excel工作簿的数据


Tags: of数据info信息dfforreadexcel
2条回答

我找到了自己解决这个问题的办法。你知道吗

    Final = pd.DataFrame(index=range(95))    #95 is the number of rows I have for each column
    n=0

    for i in filepaths:           #filepaths is a list of n filepaths 
        df = pd.read_excel(i)     #read the excel sheets`
        info = otherslices         #condensed form of added slices from df`
        Final[n]=pd.DataFrame(info)
        n+=1

    Final = Final.append(Final)  #big dataframe of n columns
    Final

我将创建一个数据帧列表,您将其附加到每个循环中,然后在循环之后将列表连接到单个数据帧中。就像这样。你知道吗

Final=[]
for i in filepaths:           #filepaths is a list of n filepaths`
    df = pd.read_excel(i) #read the excel sheets`
    info = otherslices   #condensed form of added slices from df`
    Final.append(info)  #expected big dataframe`'
Final=pd.concat(Final)

相关问题 更多 >