I have about 50 excel files and I want to import into a dataframe and merge all the files into a single data file. But in some file there are 3 some of 4 columns. Each file as different columns in a different order.
Total column from all files: 5 ie col1, col2, col3, col4, col5
I know how to import, but when I start, problems with the problem.
Script:
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)
but its throwing error "ValueError: Columns should be the same length as the key"
I want to add the data df ['col1', 'col2', 'col5'] to dfAll ['col1', 'col2', 'col5']
Please help on this issue.
source
share