I want to export excel sheets in the same excel file using python pandas: I used this code but it export only the first sheet doct.to_excel('test.xlsx')
Asked
Active
Viewed 377 times
1
-
2Does this answer your question? [Save list of DataFrames to multisheet Excel spreadsheet](https://stackoverflow.com/questions/14225676/save-list-of-dataframes-to-multisheet-excel-spreadsheet) – Shubham Periwal May 26 '21 at 13:12
-
path=r'C:\Users\msi\Desktop\MED4EBM\Excel_Data_Managed\fisheries\Mahres' – Saifeddine Farjallah May 26 '21 at 13:37
1 Answers
0
I can't see the code you mentioned. As I understand it, you would like to transform 2 spreadsheets into just one, if that is your intention first it is important to know if the order and fields of the two spreadsheets are the same. If they are the same: you can use the function concat() of pandas.
Example:
df1 = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
"C": ["C0", "C1", "C2", "C3"],
"D": ["D0", "D1", "D2", "D3"],
},
index=[0, 1, 2, 3],
)
df2 = pd.DataFrame(
{
"A": ["A4", "A5", "A6", "A7"],
"B": ["B4", "B5", "B6", "B7"],
"C": ["C4", "C5", "C6", "C7"],
"D": ["D4", "D5", "D6", "D7"],
},
index=[4, 5, 6, 7],
)
frames = [df1, df2]
result = pd.concat(frames)
result.to_excel('test.xlsx', index=False)
If are not the same, you will need to adjust the data to match the same data before concatenating.

fer
- 16
- 2