Data Inspection & Exploration

Python tutorial · PySpark.in

Data Inspection & Exploration

Data Inspection and Exploration is the process of understanding the structure, size, content, and statistical properties of a dataset before performing cleaning or modeling..

Why Do We Need Data Inspection?

✔ Understand dataset structure
✔ Detect missing values
✔ Check data types
✔ Find outliers
✔ Decide cleaning strategy

Always FIRST STEP in EDA(Exploratory Data Analysis)

Exploratory Data Analysis (EDA) is the process of analyzing, summarizing, and visualizing datasets to discover patterns, trends, relationships, and anomalies before building models.

Sample DataFrame Used

```

import pandas as pd

data = {

"Name": ["Megha", "Amit", "Riya", "Neha", "Rahul"],

"Age": [21, 22, 21, 23, 22],

"City": ["Delhi", "Mumbai", "Delhi", "Pune", "Delhi"],

"Marks": [85, 90, 88, 92, 80]

}

df = pd.DataFrame(data)

print(df)

```

1. head() and tail()

By default:

Used to quickly inspect large datasets.

```

print(df.head())

```

```

print(df.tail())

```

2. shape, size, ndim

```

print(df.shape)

```

Means: 5 rows, 4 columns

```

print(df.size)

```

Formula: rows × columns = 5 × 4 = 20

```

print(df.ndim)

```

DataFrame is always 2-Dimensional

3. info()

info() provides a complete technical summary of the DataFrame including:

Used by data scientists first to understand dataset structure and cleanliness.

```

print(df.info())

```

4. describe()

describe() generates statistical summary of numerical columns.

It includes:

```

print(df.describe())

```

Used in EDA (Exploratory Data Analysis)

5. value_counts()

value_counts() returns the frequency count of each unique value in a column.

```

print(df["City"].value_counts())

```

Used for categorical data analysis

6. unique() and nunique()

unique() returns all distinct values in a column.

```

print(df["City"].unique())

```

nunique()

nunique() returns the number of unique values.

```

print(df["City"].nunique())

```

ADVANCED (Interview Level)

Filtering

Filtering selects rows based on a condition.

```

print(df[df["Marks"] > 85])

```

Sorting

sort_values() sorts data in ascending or descending order.

```

print(df.sort_values(by="Marks", ascending=False))

```

Missing Values Handling

Checks missing (NaN) values.

```

print(df.isnull())

```

Output: all False (no missing values)

fillna()

Fills missing values with a specified value.

```

print(df.fillna(0))

```

apply()

apply() applies a custom function to each value of a column.

```

df["Updated_Marks"] = df["Marks"].apply(lambda x: x + 5)

print(df)

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges