Summary - Pandas basics
- Machine Learning course
- Beginner
- Lesson 18/112
- 6 min
In this group you learned to create DataFrames, work with columns, and select data.
Summary: pandas basics
What you learned in this group:
- The
SeriesandDataFramestructures,read_csv/to_csv. - Building a table from a dict or a list of rows.
- Selecting columns, adding computed columns,
dropandrename. - Filtering with boolean masks,
locandiloc. - 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
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8], "etiket": [0, 1, 0, 1]})
# buraya yazSample solution
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.
X = df.____()
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 |