简体   繁体   中英

Changing selected columnn types to percentage while writing pandas df to excel

I am writing multiple sheets in to excel file using pandas:

df_res = {'name' : df_1,
          'name_2': df_2}

sheet_names = ['first','second']

with pd.ExcelWriter('my_file.xlsx') as writer:  
    for new_name, (name,df) in zip(sheet_names, df_res.items()):
        df.to_excel(writer, sheet_name = new_name)

My structure of multiple dfs is that they are in a dictionary. In each of those two dfs there are certain columns which format I would like to change to a percentage - same as selecting a column in excel and click on % sign to convert it to percentage type.

I achieve the formatting by adding the following to my for loop:

workbook  = writer.book
worksheet = writer.sheets[new_name]
format_percentages = workbook.add_format({'num_format': '0%'})
# below I change a single column type
worksheet.set_column('J:J', 18, format_percentages) 

The problem is that my wanted columns do not appear in the same order. if below were my dfs :

# df_1
A               B               C               # immitating excel column names
name            sales           difference
A               10              50

# df_2
A               B               C               # immitating excel column names
name            difference      sales
B               10              50

I could not call the format on column B or C since difference which I would like to be represented as percentage type appears in different columns.

Is there a way to change column type by giving its column name?

You could try this:

import pandas as pd

df_1 = pd.DataFrame({"name": {0: "A"}, "sales": {0: 10}, "difference": {0: 50}})

df_2 = pd.DataFrame({"name": {0: "B"}, "difference": {0: 10}, "sales": {0: 50}})


df_res = {"name": df_1, "name_2": df_2}

sheet_names = ["first", "second"]

with pd.ExcelWriter("my_file.xlsx") as writer:
    for new_name, (name, df) in zip(sheet_names, df_res.items()):
        for col in ["sales", "difference"]:
            df[col] = pd.Series(
                ["{0:.2f}%".format(val) for val in df[col]], index=df.index
            )
        df.to_excel(writer, sheet_name=new_name)

Which outputs an Excel files with both columns "difference" and "sales" formatted as percentages on both sheets.

You could use columns.get_loc which giving a column name, returns the integer location for the requested label. You can then use set_column using the previous value on first_col and last_col :

To apply a style to a single column the value of first_col and last_col should be the same:
worksheet.set_column(1, 1, 30) # Width of column B set to 30.

    ...
    ...
    for new_name, (name,df) in zip(sheet_names, df_res.items()):
        workbook  = writer.book
        worksheet = writer.sheets[new_name]
        format_percentages = workbook.add_format({'num_format': '0%'})

        col_idx = df.columns.get_loc('difference')
        worksheet.set_column(col_idx, col_idx, 18, format_percentages)

        df.to_excel(writer, sheet_name = new_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM