Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving a dataframe to csv file (python)

I am trying to restructure the way my precipitations' data is being organized in an excel file. To do this, I've written the following code:

import pandas as pd

df = pd.read_excel('El Jem_Souassi.xlsx', sheetname=None, header=None)
data=df["El Jem"]

T=[]
for column in range(1,56):
    liste=data[column].tolist()
    for row in range(1,len(liste)):
        liste[row]=str(liste[row])
        if liste[row]!='nan':
            T.append(liste[row])

result=pd.DataFrame(T)
result

This code works fine and through Jupyter I can see that the result is good screenshot

However, I am facing a problem when attempting to save this dataframe to a csv file.

 result.to_csv("output.csv")

The resulting file contains the vertical index column and it seems I am unable to call for a specific cell.

(Hopefully, someone can help me with this problem) Many thanks !!

like image 965
farhat Avatar asked Apr 02 '18 09:04

farhat


People also ask

How do I save a DataFrame to a CSV file in Python?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.


1 Answers

It's all in the docs.

You are interested in skipping the index column, so do:

result.to_csv("output.csv", index=False)

If you also want to skip the header add:

result.to_csv("output.csv", index=False, header=False)


I don't know how your input data looks like (it is a good idea to make it available in your question). But note that currently you can obtain the same results just by doing:

import pandas as pd
df = pd.DataFrame([0]*16)
df.to_csv('results.csv', index=False, header=False)
like image 173
jjj Avatar answered Sep 22 '22 02:09

jjj