1

I have pandas dataframe where mutiindex is there(pic 1) but when i am converting it csv it not showing as multiindex(pic 2)

I am using to_csv() .Is there any parameter i need to pass to get right format?

pic 1: enter image description here

pic:2

enter image description here

Tried as per suggestion, below is the pic

enter image description here

3 Answers 3

1

If you're not bothered about getting a CSV as output the way I do this is by putting the data in an XLSX file.

# Create the workbook to save the data within
workbook = pd.ExcelWriter(xlsx_filename, engine='xlsxwriter')

# Create sheets in excel for data
df.to_excel(workbook, sheet_name='Sheet1')

# save the changes
workbook.save()

Can you try this and see if it formats how you want?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response but i am not getting the desired format.
0

Maybe this can be helpful for you:

    pd.DataFrame(df.columns.tolist()).T.to_csv("dataframe.csv", mode="w", header=False, index=False)

    df.to_csv("dataframe.csv", mode="a", header=False, index=False)

1 Comment

Thanks for your response. Its not working as expected
0

I guess you are using an older version of pandas. If you are using <0.21.0, there is a tupleize_cols parameter you can play with. If above, just save as to_csv. It will default to each index in one row -

enter image description here

1 Comment

Thanks for your rsponse, i tried but still in csv i am not seeing multiindex format. I have added the pic in the question itself

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.