1

I want to save an online csv file to my computer using pandas. Kindly help out.

This is my code:

import pandas as pd

url = "https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
df = pd.DataFrame(url)
df.to_csv(r"C:\Users\hp\Documents\files.csv")"

This is the bug I get:

ValueError                                Traceback (most recent call last)
<ipython-input-8-8d02ffbff077> in <module>
      2 
      3 url = "https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
----> 4 df = pd.DataFrame(url)
      5 df.to_csv(r"C:\Users\hp\Documents\files.csv")
      6 

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    483                 )
    484             else:
--> 485                 raise ValueError("DataFrame constructor not properly called!")
    486 
    487         NDFrame.__init__(self, mgr, fastpath=True)

ValueError: DataFrame constructor not properly called!
0

4 Answers 4

2

You need to use the pd.read_csv() function rather than creating a dataframe by using pd.DataFrame(). The latter will only create a dataframe when instantiated from a certain data structure like an array or dictionary.

The first will read the dataframe from your URL.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using

url = pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv")
df = pd.DataFrame(url)
display(url)

Or you can read URL as string

import io
import requests

url="https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
string = requests.get(url).content
data = pd.read_csv(io.StringIO(string.decode('utf-8')))
df = pd.DataFrame(data)
display(df)

output

Month   "1958"  "1959"  "1960"
0   JAN     340     360     417
1   FEB     318     342     391
2   MAR     362     406     419
3   APR     348     396     461
4   MAY     363     420     472
5   JUN     435     472     535

Comments

0

try

import pandas as pd

url = "https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
df = pd.read_csv(url)
df.to_csv(r"C:\Users\hp\Documents\files.csv")

Comments

0
import pandas as pd

url = "https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
path = "test_csv.csv"

pd.read_csv(url).to_csv(path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.