Save array to python CSV

Asked

Viewed 815 times

0

I wrote the code below to store some data in a dataframe, soon after I converted it into an array, but when saved the same in CSV file, the same jumps lines in saved file

import pandas as pd
import csv

df1 = pd.read_csv("CSSS.csv",  encoding="UTF-8", sep=";", usecols=["CPF"])
df2 = pd.read_csv("CSSS.csv",  encoding="UTF-8", sep=";", usecols=["Login"])

df = pd.concat([df1,df2], axis=1)
df_array = df.values


with open("crl.csv", "wt") as csv_output:
    writer = csv.writer(csv_output, delimiter=";")
    writer.writerow(["CPF", "Login"])

    for d in df_array:
        writer.writerow(d)

follows how the CSV file Arquivo CSV

1 answer

1

With the very pandas you can do it, it ta function to_csv who does exactly what you want

df.to_csv("crl.csv",index=False,sep=";")

documentation and references:

to_csv

  • until facilitated, but the data are saved in a single column separated only by comma

  • already got, thanks for the tip I used the Sep

  • df.to_csv("crl.csv", index=False, Sep=";")

  • Pasta cara, if the answer helped you mark how the answer Obs edited the answer to suit your situation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.