Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pandas - 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 !!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...