Angie
Angie

Reputation: 327

Saving my dataset as a csv file in python

I currently have a json file i downloaded from github of a dataset that I edited by adding columns of values. How would I export my newly edited dataset as a csv file that I could upload back to github?

Currently my data is saved as:

import pandas as pd

url = 'https://raw.githubusercontent.com/xxx.json' #example of raw url taken from github
df = pd.read_json(url) #dataset from json file

df['H_values'] = output #new column added of values

Since I updated the original dataset (df) with a column called "H_values" I would like to export this version of the dataset as a csv file (the last line of the code is the updated data). Thanks!

Upvotes: 0

Views: 6111

Answers (2)

Pdeuxa
Pdeuxa

Reputation: 699

Simple:

df.to_csv("output.csv")

Check the Doc on pandas librairy for more information, on the fonction and its parameter

Upvotes: 2

axelschmidt
axelschmidt

Reputation: 11

The documentation for dataframes suggest an option to convert one into a *.csv like string: Link

Now you just need to save this in the file:

with open("Output.csv", "w") as text_file:
    text_file.write(df.to_csv(index=False))

for more options I refere to the doc-link above.

Upvotes: 0

Related Questions