Pandas

Pandas is a powerful module that aids data analysis. Even if you have a simpler problem to solve, if it involves reading, writing and finding data, pandas may well be the right module for your next python program.

Like all good python modules, you can install it using pip.

Python and laptop
pip install pandas

I am particularly fond of how it easily deals with CSV files. Let’s the the following one, example.csv, which consists of a header row followed by three rows of data

Colour,Weight,Type
blue,chunky,wool 
green,sport,cotton
yellow,extra-fine,bamboo 

We can use the following simple program to read in the file and process the data.

import pandas as pd

filename = "example.csv"

domains = pd.read_csv(filename)

for index, row in domains.iterrows():
  print(row['Weight'] + " " + row['Colour'] + " yarn made from " + row['Type'])

When we run it, we see pandas was able to just show us the data rows and also allows us to access the specific columns in the rows using the header we provided.

chunky blue yarn made from wool 
sport green yarn made from cotton
extra-fine yellow yarn made from bamboo 

We’ve glossed over python versions, but if you get a ModuleNotFoundError for pandas when you have installed it, you might need to use pip3 to install pandas, assuming you are running with python3, depending on your tooling.

ModuleNotFoundError: No module named 'pandas'

Here is a method we wrote to retrieve a pandas row using our index field ‘id’. We convert the id we received from the user to ensure it is the correct type (int), otherwise we might find that 1 != ‘1’. Note that we could use any field to do this retrieval, but in our database, this is the row we know to be unique. If we retrieved b y another field, we could get back multiple rows, which in a different context, such as filtering a list, would be fine.

import pandas as pd

database = "database.csv"
idField = "id"

def getRow(id):
  df = pd.read_csv(database)
  return df.loc[df[idField] == int(id)] 

More Pandas

We explore understanding the data set in Python Pandas Max value and Pandas Axis

In Python pandas as simple DB, we will leverage pandas power to update the data, forming the start of a simple little database.