CodeBlanks Logo Icon CodeBlanks

Summary - Pandas basics

In this group you learned to create DataFrames, work with columns, and select data.

Summary: pandas basics

What you learned in this group:

  • The Series and DataFrame structures, read_csv/to_csv.
  • Building a table from a dict or a list of rows.
  • Selecting columns, adding computed columns, drop and rename.
  • Filtering with boolean masks, loc and iloc.
  • Exploring with head, info, describe, value_counts.
df = pd.read_csv("veri.csv")
X = df.drop(columns=["etiket"]).to_numpy()
y = df["etiket"].to_numpy()

The last two lines are the pattern for turning a pandas table into the X and y scikit-learn expects.

Practice X and y

Create X by dropping the etiket column from df and y from the etiket column (both with to_numpy()). Print X.shape and y.shape separated by a space.

Starter code

model.py
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8], "etiket": [0, 1, 0, 1]})

# buraya yaz

Sample solution

model.py
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8], "etiket": [0, 1, 0, 1]})

X = df.drop(columns=["etiket"]).to_numpy()
y = df["etiket"].to_numpy()
print(X.shape, y.shape)

Expected output

(4, 2) (4,)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is the right way to remove the label column from X?

  • df.drop(columns=["etiket"]) Correct answer
  • df["etiket"]
  • df.head()
  • df.rename(columns={"etiket": ""})

Explanation: drop returns all remaining columns without the label; y becomes df["etiket"].

2 Convert the table to a NumPy array.

Question
X = df.____()
Answer
X = df.to_numpy()

Explanation: to_numpy produces the numeric array to give to scikit-learn.

3 Match the jobs with the tools.

Term Meaning
Reading a file read_csv
Filtering rows df[condition]
Selecting by position iloc
Summary statistics describe

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation