Load a csv file python jupyter - python

I'm new to python and I got this error I couldn't solve
import pandas as pd
import numpy as np
url = 'http://localhost:8888/edit/Downloads/untitled.cvs'
food2014_recalls = pd.read_csv(url)
This is my csv file:
animal,uniq_id,water_need
elephant,1001,500
elephant,1002,600
elephant,1003,550
I got this error:

import pandas as pd
import numpy as np
import io
import requests
url ='http://localhost:8888/edit/Downloads/untitled.cvs'
res =requests.get(url).content
food2014_recalls =pd.read_csv(io.StringIO(res.decode('utf-8')), error_bad_lines=False, comment='#', sep=',')

Related

Problem reading CSV file from URL in pandas python

I'm trying to read csv file in pandas from this url:
https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv
By doing this:
url = "https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv">
c = pd.read_csv(url)
Or by doing this:
import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
And i still get the same error message:
ParserError: Error tokenizing data. C error: Expected 1 fields in line 3, saw 2
Simply:
import pandas as pd
df = pd.read_csv("https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv?dl=1")
print(df)
You require a ?dl=1 at the end of your link.
Related
Add ?dl=1 to the end of the URL
import pandas as pd
url = "https://www.dropbox.com/s/uh7o7uyeghqkhoy/diabetes.csv?dl=1"
c = pd.read_csv(url)
print(c)
How to download dropbox csv file to pandas

Read_csv from URL into Jupyter

Hi I am unable to read CSV file from the URL by using
import pandas as pd
import numpy as np
data_url = 'https://data.baltimorecity.gov/Financial/Real-Property-Taxes/27w9-urtv.csv'
df = pd.read_csv(data_url)
df.head()
I got an error: "not acceptable"
I also tried different codes importing "requests" but none of them worked. How do I fix this?
Your URL wasnt correct. This should work:
import pandas as pd
data_url = 'https://data.baltimorecity.gov/resource/27w9-urtv.csv'
df = pd.read_csv(data_url)
df.head()

CSV file not found despite specified path

I am trying a project as a beginner. It is driving me nuts because I keep getting minor errors that paralyze the whole execution. Here's an error that has been plaguing me.
### SOLUTION
## 1. Introduction of dataset
import pandas as pd
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
# This lets us see many columns in the output
pd.set_option('display.expand_frame_repr', False)
df = pd.read_csv('data.csv', index_col=0)
Error:
File "C:\ProgramData\Anaconda\Lib\site-packages\pandas\_libs\parsers.cp36-win_amd64.pyd", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
V\000~Ã\000\000ëtA¸P\000\000\000H»Ì\000H
builtins.FileNotFoundError: File b'data.csv' does not exist.
Why do I get this error even though the csv file exists?
You need to provide absolute path for the file.
For example: pd.read_csv("/path/to/the/file/data.csv", ...)
Or if you want to read the file from current directory:
import os
import sys
csv_path = os.path.dirname(os.path.abspath(sys.executable)) + '/data.csv'
df = pd.read_csv(csv_path, index_col=0)

Unable to read a text file into jupyter notebook on mac

I downloaded this dataset and stored it in a folder called AutomobileDataset.
I cross checked the working directory using:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
Output:
'/Users/madan/Desktop/ML/Datasets/AutomobileDataset'
Then I tried reading the file using pandas:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
automobile_data = pd.read_csv("AutomobileDataset.txt", sep = ',',
header = None, na_values = '?')
automobile_data.head()
Output:
---------------------------------------------------------------------------
ParserError: Error tokenizing data. C error: Expected 1 fields in line 2, saw 26
Someone please help me with this, I don't know where I am making a mistake.
Can try this!
import os
# Read in a plain text file
with open(os.path.join("c:user/xxx/xx", "xxx.txt"), "r") as f:
text = f.read()
print(text)

xlsxwriter issues using python 3.3 using pandas

Struggling to get xlxswriter to create multiple worksheets and use the pandas df.to_excel method to place the data in there.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import xlsxwriter
import os as os
import sqlite3 as sql
import xlrd
path = (r'C:\Users\test1\Downloads\category.xlsx') #folder
data = pd.DataFrame()
data = pd.read_excel(path,sheetname='1')
print (data.shape)
data.head()
#create excel workbook using xlsxwriter
#workbook = xlsxwriter.Workbook('hyperlink.xlsx')
writer = ExcelWriter('hyperlink.xlsx')
data.to_excel(writer,'Sheet1')
workbook.close()

Categories