CodeBlanks Logo Icon CodeBlanks

Data types

Data types (dtype) are categorized as numeric, categorical, and text.

Check the types

Numbers read from CSV sometimes arrive as text (object), because of values like "1.200", "?" or "35 kg" containing a unit. dtypes shows this:

print(df.dtypes)
df["fiyat"] = pd.to_numeric(df["fiyat"], errors="coerce")

errors="coerce" turns values that can't be converted into NaN; they are then handled as missing values.

Type conversion and categories

The type is changed with astype. Text columns with few distinct values save memory when converted to the category type:

df["yas"] = df["yas"].astype(int)
df["sehir"] = df["sehir"].astype("category")
print(df["sehir"].cat.categories)

The model still doesn't accept text; categories are encoded as numbers in the next lesson.

Practice Convert to numbers

Convert the ham list to numbers with pd.to_numeric(..., errors="coerce"); print the number of missing values with isna().sum() and the mean of the valid values with mean().

Starter code

model.py
import pandas as pd

s = pd.Series(["10", "20", "?", "30", "yok"])

# buraya yaz

Sample solution

model.py
import pandas as pd

s = pd.Series(["10", "20", "?", "30", "yok"])

x = pd.to_numeric(s, errors="coerce")
print(x.isna().sum())
print(x.mean())

Expected output

2
20.0

Questions and answers

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

1 What does <code>pd.to_numeric(["5", "?"], errors="coerce")</code> produce?

  • [5, 0]
  • ["5", "?"]
  • [5.0, NaN] Correct answer
  • An error

Explanation: The unconvertible "?" becomes NaN; because of NaN the result is float.

2 Convert text to numbers; let unconvertible ones become NaN.

Question
df["fiyat"] = pd.to_numeric(df["fiyat"], errors=____)
Answer
df["fiyat"] = pd.to_numeric(df["fiyat"], errors="coerce")

Explanation: "raise" errors, "coerce" produces NaN.

3 Match the types.

Term Meaning
int64 Integer
float64 Decimal number
object Text
category Limited set of values

All lessons in Beginner

13 Project: Classification

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