DEV Community

Cover image for Read data with pandas
petercour
petercour

Posted on

1 1

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:

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

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!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay