Ramesh Kamaraj
Ramesh Kamaraj

Reputation: 1

Trying to load the csv file from github user, however its throwing error. stating that url error

Anyone can help me fix this below error?

df=pd.read_csv('https://github.com/krameshdn/krameshdn/blob/1ad67512c81f30722831ae3e2290fc853040474a/Concrete_Data.csv')

error:

URLError: <urlopen error [Errno 11001] getaddrinfo failed>

Upvotes: 0

Views: 243

Answers (2)

darren
darren

Reputation: 5704

The problem you're having is that the url is not a csv, but a html file. In order to get the raw csv, you have to modify the url to this:

https://raw.githubusercontent.com/krameshdn/krameshdn/1ad67512c81f30722831ae3e2290fc853040474a/Concrete_Data.csv

So here is the full code (with the ammended url explicitly shown):

import pandas as pd

# this does not work
url = 'https://github.com/krameshdn/krameshdn/blob/1ad67512c81f30722831ae3e2290fc853040474a/Concrete_Data.csv'
# convert url to raw and this works
url = 'https://raw.githubusercontent.com/krameshdn/krameshdn/1ad67512c81f30722831ae3e2290fc853040474a/Concrete_Data.csv' 

df=pd.read_csv(url)

Here is the result:

      Cement  Blast  Fly Ash  Water  Superplasticizer      CA     FA  Age    CMS
0      540.0    0.0      0.0  162.0               2.5  1040.0  676.0   28  79.99
1      540.0    0.0      0.0  162.0               2.5  1055.0  676.0   28  61.89
2      332.5  142.5      0.0  228.0               0.0   932.0  594.0  270  40.27
3      332.5  142.5      0.0  228.0               0.0   932.0  594.0  365  41.05
4      198.6  132.4      0.0  192.0               0.0   978.4  825.5  360  44.30
...      ...    ...      ...    ...               ...     ...    ...  ...    ...
1025   276.4  116.0     90.3  179.6               8.9   870.1  768.3   28  44.28
1026   322.2    0.0    115.6  196.0              10.4   817.9  813.4   28  31.18
1027   148.5  139.4    108.6  192.7               6.1   892.4  780.0   28  23.70
1028   159.1  186.7      0.0  175.6              11.3   989.6  788.9   28  32.77
1029   260.9  100.5     78.3  200.6               8.6   864.5  761.5   28  32.40

[1030 rows x 9 columns]

Upvotes: 0

LoopFree
LoopFree

Reputation: 149

You can do it with request module along with csv module

import csv
import urllib.request

url = "https://github.com/krameshdn/krameshdn/blob/1ad67512c81f30722831ae3e2290fc853040474a/Concrete_Data.csv"
response = urllib.request.urlopen(url)
data = csv.reader(lines)

for row in data:
    print(row)

Upvotes: 1

Related Questions