How To Load Data With Python And Pandas
Load Data With Pandas
Pandas is one of the most popular data manipulation libraries in python, so in this post, let’s take a look at the most common ways to load data into a pandas dataframe.
Load CSV
First up, let’s try loading data from a csv.
Loading data from a csv is probably the most popular way of getting your data into pandas.
Pandas makes it easy for us with the read_csv command.
import pandas as pd
csv_path = "path_to_csv_file"
df = pd.read_csv(csv_path)
Load Excel
Another common file type for tabular data is excel. Let’s take a look at how to load an excel spreadsheet.
import pandas as pd
excel_path = "path_to_excel_file"
df = pd.read_excel(excel_path)
Load JSON
Json is commonly used in applications on the web. Most web api’s communicate via Json nowadays. Let’s take a look at how to get Json into our dataframe.
import pandas as pd
json_path = "path_to_json_file"
df = pd.read_json(json_path)
Load By SQL
Lastly, relational databases are super common in software. Using sqlalchemy, let’s take a look at how to query data from our database.
import pandas as pd
from sqlalchemy import create_engine
conn = create_engine('sqlite:///test.db')
df = pd.read_sql_query('SELECT * FROM table_name', conn)