Using Pandas for Complex Data Instead of Spreadsheets

The Pain of Using Spreadsheets

Spreadsheets are great, but they can become a pain when you are dealing with complex data:

  • Calculations are often not reproducible.
  • Data can be overwritten in the spreadsheet.
  • Data cleaning may overwrite the original data.
  • Sharing spreadsheets is difficult.
  • Combining data from multiple spreadsheets is difficult.
  • Spreadsheets often demonstrate poor performance.
  • Large datasets are not handled well.

Pandas to the Rescue

Fortunately, we have Pandas to help us mung data on Python.

  • Pandas is one of the most powerful open source libraries in Python for analyzing and manipulating data.
  • This library was born on 2008 at AQR Capital when Wes McKinney was looking for a solution to offer a high-performance and flexible tool to perform quantitative analysis on financial data.
  • Etymology: panel data structures

Why Pandas is Great

  • Python + Pandas = the perfect combination for small experiments or for implementing large-scale production systems to analyze data and make smarter decisions.
  • High-performance data structures:
    • Series (1D labeled vectors)
    • DataFrame (2D structures similar to spreadsheets)
    • Panel (Collection of DataFrames as 3D labeled arrays)
  • Built-in time series functionality, which is a must for financial and quants analysis

Reading a CSV file as a Pandas DataFrame

Use the following CSV files for this activity:

In [1]:

# Initial imports
import pandas as pd
from pathlib import Path

Step 1: Create a path to the file

In [2]:

csvpath = Path("../Resources/sales.csv")

Step 2: Read the CSV into a DataFrame using Pandas

In [3]:

sales_dataframe = pd.read_csv(csvpath)
sales_dataframe.head()

Out [3]:

“\n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”,
FullNameEmailAddressZipCreditCardSalePrice
0Elwanda Whitealyre2036@live.com352 Lakeshore Mall92365327 0855 9720 705584.33
1Lyndon Elliottarrowy1873@outlook.com1234 Avery Plaza13303717 498777 19636879.95
2Daisey Sellerstoucan2024@outlook.com469 Elwood Street76313758 579477 35734907.58
3Issac Reevesasarin1958@gmail.com565 Phelps Field811684400 0380 4162 1622545.88
4Bradford Kinneymibound1801@yandex.com853 Mission Rock Freeway417213712 263405 60178517.49

Reading a CSV with no Header

In [4]:

# Without a header in the CSV, Pandas will use the first row as the header
csvpath = Path("../Resources/sales_no_header.csv")
sales_data = pd.read_csv(csvpath)
sales_data.head()

Out [4]:

“\n”, “\n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”,
Elwanda Whitealyre2036@live.com352 Lakeshore Mall92365327 0855 9720 705584.33
0Lyndon Elliottarrowy1873@outlook.com1234 Avery Plaza13303717 498777 19636879.95
1Daisey Sellerstoucan2024@outlook.com469 Elwood Street76313758 579477 35734907.58
2Issac Reevesasarin1958@gmail.com565 Phelps Field811684400 0380 4162 1622545.88
3Bradford Kinneymibound1801@yandex.com853 Mission Rock Freeway417213712 263405 60178517.49
4Fermina Cobbkingfisher2013@live.com929 Prague Trail166252351 7156 8193 8639889.95

In [5]:

# Read the file without a header and set header=none
sales_data = pd.read_csv(csvpath, header=None)
sales_data.head()

Out [5]:

“\n”, “\n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”,
012345
0Elwanda Whitealyre2036@live.com352 Lakeshore Mall92365327 0855 9720 705584.33
1Lyndon Elliottarrowy1873@outlook.com1234 Avery Plaza13303717 498777 19636879.95
2Daisey Sellerstoucan2024@outlook.com469 Elwood Street76313758 579477 35734907.58
3Issac Reevesasarin1958@gmail.com565 Phelps Field811684400 0380 4162 1622545.88
4Bradford Kinneymibound1801@yandex.com853 Mission Rock Freeway417213712 263405 60178517.49

In [6]:

# Rewrite the column names
columns = ["Full Name", "Email", "Address", "Zip Code", "Credit Card Number", "Sale Price"]
sales_data.columns = columns
sales_data.head()

Out [6]:

“\n”, “\n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”,
Full NameEmailAddressZip CodeCredit Card NumberSale Price
0Elwanda Whitealyre2036@live.com352 Lakeshore Mall92365327 0855 9720 705584.33
1Lyndon Elliottarrowy1873@outlook.com1234 Avery Plaza13303717 498777 19636879.95
2Daisey Sellerstoucan2024@outlook.com469 Elwood Street76313758 579477 35734907.58
3Issac Reevesasarin1958@gmail.com565 Phelps Field811684400 0380 4162 1622545.88
4Bradford Kinneymibound1801@yandex.com853 Mission Rock Freeway417213712 263405 60178517.49

In [7]:

# Generate summary statistics
sales_data.describe()

Out [7]:

“\n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”, ” \n”,
Zip CodeSale Price
count100.000000100.000000
mean40952.160000533.007200
std30207.118496275.531072
min555.00000029.720000
25%11109.750000328.507500
50%40033.500000536.110000
75%65834.750000767.885000
max99877.000000998.760000

We will be happy to hear your thoughts

Leave a reply