0

I'm tring to collect some data from my db, and using pandas to make a excel file. The dataframe structure I got like below:

>>> df

                                            2020-05-21~2020-05-27
input_cost   jim    bob     companyc            17770.65                17770.65 
                            companye            3392.73                 3392.73
                            companyd            2892.30                 2892.30
                            companyf            22843.67                22843.67
             casy   dave    company_a           160.93                  160.93                                                      
                            company_b           160.93                  160.93

And then I want to add a header to it , then export it to excel file by using df.to_excel

# expected output
                                                date
data_source  sale   leader  entity_name       2020-05-21~2020-05-27       sum 
input_cost   jim    bob     companyc            17770.65                17770.65
                            companye            3392.73                 3392.73
                            companyd            2892.30                 2892.30
                            companyf            22843.67                22843.67
             casy   dave    company_a           160.93                  160.93                                                                     
                            company_b           160.93                  160.93

Sorry that I'm a new bee to pandas, how can I make it work?

Thanks.

1 Answer 1

1

You can use rename_axis:

# rename index
df = df.rename_axis(['data_source', 'sale', 'leader', 'entity_name'])

# add level to columns
df.columns = pd.MultiIndex.from_product([['date'], df.columns])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply, I tried but got some error like NotImplementedError: isna is not defined for MultiIndex
And my colums is like MultiIndex([('2020-05-21~2020-05-27',)],)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.