Python ML Pandas DataFrames for Machine Learning
A pandas DataFrame is a table with named columns and numbered rows. It is the main way machine learning data is held in Python.
Load a table and read its size
The table holds 5 rows and 4 columns. The columns are name, age, city, and price.
import io
import pandas as pd
text = "name,age,city,price\nada,28,paris,120\nbob,35,lyon,200\ncy,25,paris,90\ndee,40,lyon,150\neli,30,paris,\n"
df = pd.read_csv(io.StringIO(text))
print(df.shape)
print(list(df.columns))Keep only the Paris rows
The test keeps rows where the city is Paris. Three rows match.
import io
import pandas as pd
text = "name,age,city,price\nada,28,paris,120\nbob,35,lyon,200\ncy,25,paris,90\ndee,40,lyon,150\neli,30,paris,\n"
df = pd.read_csv(io.StringIO(text))
paris = df[df["city"] == "paris"]
print(len(paris))
print(list(paris["name"]))Count the gaps, then fill them
One price is missing. Filling it with the mean price of 140.0 leaves no gaps.
import io
import pandas as pd
text = "name,age,city,price\nada,28,paris,120\nbob,35,lyon,200\ncy,25,paris,90\ndee,40,lyon,150\neli,30,paris,\n"
df = pd.read_csv(io.StringIO(text))
print(df["price"].isna().sum())
print(df["price"].fillna(df["price"].mean()).tolist())One mean per city
Groupby splits the table by city. Lyon averages 175.0 and Paris averages 105.0.
import io
import pandas as pd
text = "name,age,city,price\nada,28,paris,120\nbob,35,lyon,200\ncy,25,paris,90\ndee,40,lyon,150\neli,30,paris,\n"
df = pd.read_csv(io.StringIO(text))
print(df.groupby("city")["price"].mean().round(1).to_dict())Example
import io
import pandas as pd
text = "name,age,city,price\nada,28,paris,120\nbob,35,lyon,200\ncy,25,paris,90\ndee,40,lyon,150\neli,30,paris,\n"
df = pd.read_csv(io.StringIO(text))
print("Shape:", df.shape)
print("Missing prices:", int(df["price"].isna().sum()))
mean_price = df["price"].mean()
print("Mean price:", mean_price)
df["price"] = df["price"].fillna(mean_price)
print("Prices:", df["price"].tolist())
print("Mean price by city:", df.groupby("city")["price"].mean().round(1).to_dict())Key points
- DataFrame: a table with named columns and numbered rows
- Column: one named measurement in a table, like price
- Row: one entry in a table, like one shop item
- Missing value: an empty cell, shown as NaN in pandas
- Use pd.read_csv() to load a table from a CSV file
- Use df[df["city"] == "paris"] to keep only the rows that match a test
- Use fillna() to fill missing values, and groupby() to summarise by one column
In this example, the table loads from five lines of text. One price is missing, and the mean price of 140.0 fills it. The filled prices print as a list of five numbers. The group means show Lyon at 175.0 and Paris at 116.7. A DataFrame is one table, and the same table answers every question in this lesson.
Note
A CSV file is plain text with commas between values. The first line names the columns.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does df["price"].isna().sum() count?