I have an excel sheet with several headers, for example:
_________________________________________________________________________ ____|_____| Header1 | Header2 | Header3 | ColX|ColY |ColA|ColB|ColC|ColD||ColD|ColE|ColF|ColG||ColH|ColI|ColJ|ColDK| 1 | ds | 5 | 6 |9 |10 | ....................................... 2 | dh | .......................................................... 3 | ge | .......................................................... 4 | ew | .......................................................... 5 | er | ..........................................................
Now you can see that the first two columns have no headers, they are empty, and the other columns have headers such as Header1, Header2 and Header3. Therefore, I want to read this sheet and combine it with another sheet with a similar structure.
I want to combine it in the first column of "ColX". Now I am doing this:
import pandas as pd totalMergedSheet = pd.DataFrame([1,2,3,4,5], columns=['ColX']) file = pd.ExcelFile('ExcelFile.xlsx') for i in range (1, len(file.sheet_names)): df1 = file.parse(file.sheet_names[i-1]) df2 = file.parse(file.sheet_names[i]) newMergedSheet = pd.merge(df1, df2, on='ColX') totalMergedSheet = pd.merge(totalMergedSheet, newMergedSheet, on='ColX')
But I do not know that he is not reading the columns correctly, and I think that they will not return the results the way I want. So, I want the resulting frame to be as follows:
________________________________________________________________________________________________________ ____|_____| Header1 | Header2 | Header3 | Header4 | Header5 | ColX|ColY |ColA|ColB|ColC|ColD||ColD|ColE|ColF|ColG||ColH|ColI|ColJ|ColK| ColL|ColM|ColN|ColO||ColP|ColQ|ColR|ColS| 1 | ds | 5 | 6 |9 |10 | .................................................................................. 2 | dh | ................................................................................... 3 | ge | .................................................................................... 4 | ew | ................................................................................... 5 | er | ......................................................................................
Any suggestions please. Thanks.
source share