我有大约50个excel文件,我想导入dataframe并将所有文件合并到单个dataframe中。 但有些文件有3列,有些是4列。每个文件以不同的顺序作为不同的列。在
所有文件的全部不同列:5,即col1、col2、col3、col4、col5
我知道如何导入,但在附加时面临问题。在
脚本:
dfAll = pd.DataFrame(columns=['col1', 'col2', 'col3', 'col4', 'col5')]
df= pd.read_excel('FilePath', sheetname='data1') # contains 3 columns i.e col1, col2, col5
columnsOFdf = df.columns
dfAll[columnsOFdf] = dfAll.append(df)
但它给出的错误是“ValueError:Columns must be same length as key”
我想将df['col1','col2','col5']数据附加到dfAll['col1','col2','col5']
请帮助解决这个问题。在
一种解决方案是将空列添加到从Excel文件加载的数据帧中:
试试这个:
我希望这对你有帮助。在
连接将匹配您的列
此外,每次将数据帧附加到现有帧后,它都会返回一个copy。这将严重降低性能,称为二次拷贝。最好创建一个包含所有数据帧的列表,然后连接结果。在
相关问题 更多 >
编程相关推荐