也许是一个简单的解决办法。我希望从目录中的许多相同风格的excel工作簿中提取特定信息,并将所有的特定信息连接到一个工作簿中(同时更改格式)。除了从不同的工作簿(与读取的xlsx文件的数量成比例)成功地创建了一个n列的大型数据帧之外,我已经完成了这个任务的每个部分。每个已读工作簿只有一个工作表“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工作簿的数据
发布于 2019-04-16 04:05:33
我找到了我自己解决这个问题的方法。
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
发布于 2019-04-09 20:50:10
我将创建一个数据帧列表,您可以将其附加到每个循环中,然后在循环之后将该列表连接到单个数据帧中。所以就像这样。
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)
https://stackoverflow.com/questions/55593109
复制相似问题