How to maintain mutiindex view in pandas while converting pandas dataframe to csv file? - pandas

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:
pic:2
Tried as per suggestion, below is the pic

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?

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)

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 -

Related

xlsxwriter modifying worksheet font

I need assistance modifying the font and size of excel spreadsheet from default to 'Arial' 9. I'm able to modify the header row, but unable to get the body of the spreadsheet to do the same. I'm using xlsxwriter, I'm sure it's something simple just not that familiar with xlsxwriter. See code below, any assistance greatly appreciated.
Pipeline_details.to_excel(writer1,
sheet_name ='Pipeline_Details',
startrow=1,
startcol=0,
header=False,
index=False)
workbook = writer1.book
worksheet = writer1.sheets['Pipeline Details']
(max_row, max_col) = Pipeline_details.shape
worksheet.autofilter(0,0,max_row, max_col - 1)
worksheer.hide_gridlines(2)
header_format = workbook_add.format({
'font_name': 'Arial',
'font_size': 9,
'bold': False,
'text_wrap': True})
for col_num, value in enumerate(Pipeline_details.columns.values):
worksheet.write(0, col_num, value, header_format)
cell_format = workbook.add_format({'font_name': 'Arial', 'font_size': 9})
writer1.save()
The docs for formatting while using pandas with xlsxwriter state the following:
XlsxWriter and Pandas provide very little support for formatting the output data from a dataframe apart from default formatting such as the header and index cells and any cells that contain dates or datetimes. In addition it isn’t possible to format any cells that already have a default format applied.
If you require very controlled formatting of the dataframe output then you would probably be better off using Xlsxwriter directly with raw data taken from Pandas.
That being said
It is possible to format any other, non date/datetime column data using set_column()
For an example using A notation, you'd execute this after writing your data to the worksheet:
worksheet.set_column('A:Z', None, format)
I'd try that first to see if you get anywhere with it. Otherwise I'd suggest writing your rows yourself and adding your format that way.

Not able to read the .xlsx file to pandas dataframe

enter image description here
I have succesfully pulled the required data to a .xlsx worksheet. But when I am going to convert the data to pandas dataframe an error pops out. How to solve the issue?
You could try without using 'b' while opening that file
output = open('test.xlsx','w')

Missing header values while using to_csv function

I am trying to read excel files using python and writing each sheet's data into separate csv file.
I am able to write the whole data but its just I am unable to get the header values.
Is there any way this can be resolved.
Here is the piece of code I am writing.
xyz.to_csv(f, index=False, header=None)

Pandas: printing to csv also creates an unlabelled column with row indices

I use the following code to print a dataframe into a csv file based on the answers seen at this question:
How to avoid Python/Pandas creating an index in a saved csv?
y_insincere = [classify_text(text,trimmed_posterior_dict)<0 for text in X_test]
X_output = pd.DataFrame()
X_output['number'] = number
X_output['CASE'] = case
X_output.to_csv('submission.csv',header=True,columns = ['id','case'],index='False')
However, when I look at the csv file it has an extra column without a header with row indices. I tried other fixes from the above question, but nothing worked. I am stuck. Any help appreciated

Pandas Add Timestamp to file name during saving Data Frame in Excel

While i am saving Data Frames in Excel format ("*.xlsx"), i would like to add time stamp to Excel file name.
How can i add run time time stamp to Data Frame while saving in Excel format while saving file using Pandas? Thank you
You can use datetime module. It's one of the python standard library, you can access it by pd.datetime like below.
import pandas as pd
writer = pd.ExcelWriter('output_{}.xlsx'.format(pd.datetime.today().strftime('%y%m%d-%H%M%S')))
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
writer.save()
And you can check format code table for the detail.