- It seems easiest to use
pandas.read_excel
with sheet_name=None
.
- Specify
None
to get all sheets.
- This will create a
dict
of dataframes with the sheet_names
as the keys.
- Iterate through the
keys
and values
, to combine the two columns and write back to a file.
- As per your comment, the
.fillna()
method may be used on either column, as shown in the following code.
Write just the new combined column back to the sheet
import pandas as pd
# create dict of dataframes
df = pd.read_excel('test.xlsx', sheet_name=None)
# iterate through dict
for k, v in df.items():
with pd.ExcelWriter('combined.xlsx', mode='a', engine='openpyxl') as writer: # write sheets to a new file
combined = v.iloc[:, 0].fillna('xxx') + ' ' + v.iloc[:, 1].fillna('xxx') # combine the first and second column
combined.to_excel(writer, sheet_name=k, index=False, header=False)
Write the original, and the combined columns back to the sheet
df = pd.read_excel('test.xlsx', sheet_name=None)
for k, v in df.items():
with pd.ExcelWriter('combined.xlsx', mode='a', engine='openpyxl') as writer: # write sheets to a new file
v['comb'] = v.iloc[:, 0].fillna('xxx') + ' ' + v.iloc[:, 1].fillna('xxx') # combine the first and second column
v.to_excel(writer, sheet_name=k, index=False, header=False)