Reading data from URL using Python

Reading data from URL using Python

- 1 min

What is a data set?

According Wikipedia

data set is a collection of data. Most commonly a data set corresponds to the contents of a single database table, or a single statistical data matrix, where every column of the table represents a particular variable, and each row corresponds to a given member of the data set in question.

We can find a data set in many kind of files like CSV,XML,HTML,XLS just for say a few. In this post we’re going to use a CSV.


What do you need?


Let’s do it

First of all open your text editor and create a new python file. I always recommend Atom or Visual studio code both are free.

We need to import Pandas which is a powerfull library written for data manipulation and analysis.

import pandas as pd

Then we define the url of our data set, in this case i’m using a data set of winter olympics medals, you can use whatever you want.

dataset_url="http://winterolympicsmedals.com/medals.csv"

Now we have to read our data set with pandas, this is very easy beacause Pandas does all the hard work. So we onlye have to type this lines of code:

dataset=pd.read_csv(dataset_url)

¡And that’s it! If you want to verify that all works fine, add this line to get data head of our data set:

print(dataset.head())

dataset-head

Or this to get data tail:

print(dataset.tail())

Your completly code should look like this:

import pandas as pd
dataset_url="http://winterolympicsmedals.com/medals.csv"
dataset=pd.read_csv(dataset_url)
print(dataset.head())

You can find this code on my Github

Mauricio Flores

Mauricio Flores

An engineering student in information technology and communications.

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora