Read data with pandas
petercour

petercour @petercour

Joined:
Mar 13, 2019

Read data with pandas

Publish Date: Jul 24 '19
9 2

In Python there's a module that helps you parse data. Data can be in many forms (files, tables, excel, sql, json). There exists so many data sources for historic reasons.

That module to work with data is named the pandas module.

You may know you can use the Pandas module for data analysis. But did you know there are many ways to read data?

pd.read_csv(filename)
pd.read_table(filename)
pd.read_excel(filename)
pd.read_sql(query, connection_object)
pd.read_json(json_string)
pd.read_html(url)
pd.read_clipboard()
Enter fullscreen mode Exit fullscreen mode

Yes, you can read data from many sources. These methods allow you to quickly grab your data

#!/usr/bin/python3 
import pandas as pd
data = pd.read_csv('yourfile.csv', header=None)
Enter fullscreen mode Exit fullscreen mode

If you are using MySQL as source

#!/usr/bin/python3
db = MySQLDatabase(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME)
db_work_view = db.get_work_view()
connection = db_work_view._db_connection
df_people = pd.read_sql('select * from people', connection)
Enter fullscreen mode Exit fullscreen mode

Reading an excel file

df = pd.read_excel('File.xlsx', sheetname='Sheet1')
Enter fullscreen mode Exit fullscreen mode

Well you get the idea. Pandas allows you to quickly fetch data from different data sources. It includes most of the existing data sources.

Related links:

Comments 2 total

  • Juan Carlos
    Juan CarlosJul 24, 2019
    Library Speed
    Pandas read_csv() 20.09
    NumPy fromfile() 3.88
    NumPy genfromtxt() 4.00
    NumPy loadtxt() 1.26
    csv (std lib) 0.40
    csv (list) 0.38
    csv (map) 0.37
    Faster_than_csv 0.10
Add comment