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!
Related
I am currently busy with a Plotly Dash Web Application and busy creating a dropdown referencing a column from a pandas dataframe I am reading in from a CSV file.
The issue is it is not able to read the column and I have seen it is because the column is actually a reference of another sheet I.e =RawData!A1.
I have managed to print the column so I know it exists in the dataframe and all the data is printing correctly however, Plotly Dash does not want to populate the dropdown with the label and values - my current line of code is:
options=[{'label': i, 'value': i} for i in df.CategoryName.unique()],
Category name in Google Sheets is referring to =RawData!A1
What I have tested:
Ammended my sheet name to read directly from my RawData sheet and it works fine - This is not a solution that I want though, this lead me to see the issue was with the reading from the referenced column.
Attempted using column index instead:
options=[{'label': i, 'value': i} for i in df.iloc[:,1].unique()],
Again this worked for printing but not to populate the dropdown in plotly dash.
Any advise will be greatly appreciated!
So by actually adding some data cleaning to remove rows at the bottom of my dataset in pandas it fixed my issue.
I added a remove nan based on my column CategoryName and by doing that my dropdown worked
df = df[df['CategoryName'].notna()]
The reason it worked makes sense - I setup my copy form to =RawData!A:A so my RawData at the moment only comprising of 123 rows, by row 125 in my reference sheet it was referencing a blank column causing the dropdown to have an error showing a reference to something that does not exist, very funny error but logical at the same time. Not sure if this will help many people but hopefully it will assist somebody!
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 -
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.
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')
My English is not well,sorry about that.
I am trying to sort a sheet using openpyxl and Python. I have read the documents and I don't quite understand this page. I am expecting to sort my excel file by some columns,at frist sort by column A,then by column B,then by cloumn C and last by column F.My problem is how to use add_sort_condition and add_filter_column these two ways,just the simple way would be great.If i can get an example,that's help me a lot!
wb_read = openpyxl.load_workbook(filename)
sheet_read = wb_read.get_active_sheet()
sheet_read.auto_filter.ref = 'A3:Q40000'
"""sheet_read.auto_filter.add_filter.column(2,['id'],blank=False) Didn't work
sheet_read.auto_filter.add_sort_condition('A') How and Where to use condition?
sheet_read.auto_filter.add_sort_condition('A3:Q40000') It seems didn't work again"""
wb_read.save('sorted.xlsx')
By the way,If i want to sort,Do I have to use auto_filter?