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)
Related
ValueError: Excel file format cannot be determined, you must specify an engine manually.
I want sheet name of csv file using python
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.
I am reading from a .txt file into a dataframe using pandas read_csv(). The data is in this form in the txt file (this is a sample):
abcd,efgh,ijklm
qwerty,uoipqwrt
mznxlakspqowieu
As you can see there are different number of commas in each line. But ultimately I want to put each line of the text file in a single column. How can this be achieved?
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')