What is Pandas?
- Machine Learning course
- Beginner
- Lesson 13/112
- 5 min
Pandas is a Python library for data analysis, providing DataFrame and Series data structures.
What is pandas?
pandas is the standard Python library for working with tabular data. It has two core structures:
- Series: a single labelled column.
- DataFrame: a table made of named columns.
import pandas as pd
s = pd.Series([3, 5, 8], name="puan")
print(s.mean()) # 5.333...Reading CSV, filling missing values, grouping and so on are all a few lines in pandas; then the data becomes a NumPy array via df.values and is given to the model.
Reading CSV
Real data usually comes from a file:
df = pd.read_csv("evler.csv")
print(df.shape) # (rows, columns)
print(df.columns.tolist()) # column names
df.to_csv("temiz.csv", index=False)index=False stops the row numbers from being written to the file as a separate column.
Practice Series mean
Create a pd.Series from the puan list; print its mean and its maximum separated by a space.
Starter code
import pandas as pd
puan = [70, 85, 90, 75]
# buraya yazSample solution
import pandas as pd
puan = [70, 85, 90, 75]
s = pd.Series(puan)
print(s.mean(), s.max())Expected output
80.0 90
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What type is a single column of a DataFrame?
- ndarray
- Series Correct answer
- list
- dict
Explanation: df["column"] returns a Series; a Series carries a NumPy array inside.
2 Read the CSV file.
df = pd.____("veri.csv")
df = pd.read_csv("veri.csv")
Explanation: read_csv loads the file as a DataFrame.
3 Match the structures.
| Term | Meaning |
|---|---|
Series |
Single column |
DataFrame |
Table |
read_csv |
Reading from a file |
to_csv |
Writing to a file |