I have extracted xlsx data into pandas dataframe and used style.format to format particular columns into percentages and dollars. So now my dataframe is converted to styler object, because I need to parse this data into csv. I have to convert this object into dataframe please help.
below is the code and output:
import pandas as pd
import numpy as np
file_path = "./sample_data.xlsx"
df = pd.read_excel(file_path, sheet_name = "Channel",skiprows=10, header =
[0,1,2])
dollar_cols = ['SalesTY', 'SalesLY','InStoreTY', 'InStoreLY','eCommTY']
dollar_dict = {}
for dollar_col in dollar_cols:
formatdict[dollar_col] = "${:,.0f}"
final_df = df.style.format(formatdict)
Here final_df has the columns converted to dollars but I am unable to convert this to csv or into a data frame. It's a styler object now, I need to convert this into a data frame again. Any help is appreciated. Thanks.
to_frame() function is used to convert the given series object to a dataframe. Parameter : name : The passed name should substitute for the series name (if it has one). Example #1: Use Series.
Style property returns a styler object which provides many options for formatting and displaying dataframes. A styler object is basically a dataframe with some style. In this article, we will go through 10 examples to master how styling works.
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
You can retrieve the original dataframe from the styler object using the "data" attribute.
In your example:
df = final_df.data
type(df)
yields
pandas.core.frame.DataFrame
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With