也许很容易解决。 我希望从目录中许多相同样式的 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
... ...
在循环中使用"打印(最终)"时,我目前得到的是
Excel1
info1a
info2a
info3a
...
Excel2
info1b
info2b
info3b
...
|
Excel(n)
但是,我从此循环中获得的数据帧(当我键入"最终"时)仅是 最后一个 Excel 工作簿的数据
我会创建一个数据框列表,您可以在每个循环中附加该列表,然后在循环之后将列表合并为单个数据框。所以像这样的事情。
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)
我发现了我自己的这个问题的解决方案。
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