Not able to read the .xlsx file to pandas dataframe - pandas

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')

Related

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

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 -

Use pandas to append a .xslx file is overwriting it?

I have a very simple code and I cannot figure out why it isn't working. Consider the dataframes below,
test1 = pd.DataFrame({'A':[1,2],'B':[3,4]})
test2 = test1+3
I would like to export these into two separate sheets in an excel spreadsheet so I wrote the code
with pd.ExcelWriter(path+'.xlsx',mode='W') as writer:
test1.to_excel(writer,sheet_name='Sheet1')
with pd.ExcelWriter(path+'.xlsx',mode='A') as writer:
test2.to_excel(writer,sheet_name='Sheet2')
where the path is just a string to the folder. Running this returns a single spreadsheet labeled 'Sheet2' and has the data from table2. Evidently it overwrote it and didn't append it. Why? How can I solve this?
Of course I can write
with pd.ExcelWriter(path+'.xlsx',mode='W') as writer:
test1.to_excel(writer,sheet_name='Sheet1')
test2.to_excel(writer,sheet_name='Sheet2')
which will generate a spreadsheet with both sheet1 and sheet2 and the data stored properly, but I would like to get this append to work because I am trying to generalize to many more dataframes.

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 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.

to_excel showed "No Excel writer 'openpyxl'"

I was trying to save a DataFrame data to Excel. Here is what I used
(df is the data and it contains 82 rows x 7 columns)
df.to_excel('output_test.xlsx', sheet_name='Sheet1')
but the output gave me this:
No Excel writer 'openpyxl'
I have installed openpyxl and it can be imported. Could someone help me solve this problem?
Thanks a lot!