DEV Community

Cover image for Read data with pandas
petercour
petercour

Posted on

Read data with pandas

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:

Top comments (2)

Collapse
 
juancarlospaco profile image
Juan Carlos
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
Collapse
 
petercour profile image
petercour

Thanks!