Read data with pandas

petercour

petercour

Posted on July 24, 2019

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:

💖 💪 🙅 🚩
petercour
petercour

Posted on July 24, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related