Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert data frame styler object into dataframe in python

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.

like image 970
Roohi Avatar asked Mar 20 '19 09:03

Roohi


People also ask

How can we convert a Python series object into a DataFrame?

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.

What is a styler object Python?

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.

Can a DataFrame be an object?

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.


1 Answers

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

like image 82
tomanizer Avatar answered Sep 21 '22 06:09

tomanizer