Advance Pandas

Python tutorial · PySpark.in

MultiIndex (Hierarchical Indexing)

MultiIndex allows multiple index levels in rows or columns. It is used when data has more than one dimension.

Example

```

import pandas as pd

arrays = [

['India', 'India', 'USA', 'USA'],

['Delhi', 'Mumbai', 'NY', 'LA']

]

index = pd.MultiIndex.from_arrays(arrays, names=('Country', 'City'))

df = pd.DataFrame({'Sales': [100, 200, 150, 300]}, index=index)

print(df)

```

Access Data

```

df.loc['India']

df.loc[('USA', 'LA')]

print(df)

```

Pivot Tables

A pivot table summarizes data using aggregation. Pivot tables are used for summary reports & dashboards.

Example

```

data = {

'City': ['Delhi', 'Delhi', 'Mumbai', 'Mumbai'],

'Year': [2023, 2024, 2023, 2024],

'Sales': [100, 150, 200, 250]

}

df = pd.DataFrame(data)

pd.pivot_table(df, values='Sales', index='City', columns='Year', aggfunc='sum'

```

Example

```

df.pivot(index='City', columns='Year', values='Sales')

print(df)

```

It fails if there is duplicates data exist.

Crosstab

crosstab() computes frequency tables. Crosstab is used in EDA for categorical analysis.

Step 1: Create a DataFrame

```

import pandas as pd

df = pd.DataFrame({

'City': ['Delhi', 'Delhi', 'Mumbai', 'Mumbai', 'Chennai'],

'Year': [2022, 2023, 2022, 2023, 2022]

})

print(df)

```

Step 2: Apply crosstab()

```

ct = pd.crosstab(df['City'], df['Year'])

print(ct)

```

Explanation

What crosstab() does:

Here it means:

Reshaping Data

Sample DataFrame

```

import pandas as pd

df = pd.DataFrame({

'City': ['Delhi', 'Mumbai', 'Chennai'],

'Year': [2022, 2022, 2022],

'Sales': [100, 150, 120]

})

print(df)

```

melt() → Wide to Long

```

df_melt = pd.melt(df, id_vars='City', value_vars=['Sales'])

print(df_melt)

```

Explanation (Easy)

ML models prefer LONG format, so melt() is very important.

stack() → Columns to Rows

First, create a pivot table

```

pivot = pd.pivot_table(

df,

values='Sales',

index='City',

columns='Year'

)

print(pivot)

```

Apply stack()

```

pivot_stack = pivot.stack()

print(pivot_stack)

```

Explanation

stack() = Columns ➝ Rows

unstack() → Rows to Columns

```

x = pivot.stack().unstack()

print(x)

```

Explanation

unstack() = Rows ➝ Columns

Vectorization

Performing operations on entire arrays instead of loops. Vectorization improves speed & performance.

Bad Way

```

for i in df['Sales']:

print(i * 2)

```

Good Way

```

df['Sales'] = df['Sales'] * 2

print(df)

```

Performance Optimization

Techniques

✔ Vectorization
✔ Use categorical dtype
✔ Avoid loops
✔ Use .loc[]

Example

```

df['City'] = df['City'].astype('category')

print(df)

```

Memory Management

Optimizing memory usage for large datasets. Memory optimization is critical for big data processing.

Example

```

x=df.info(memory_usage='deep')

print(x)

```

Reduce Memory

```

df['Sales'] = df['Sales'].astype('int32')

print(df)

```

Window Functions

Window functions perform calculations over a sliding window of rows instead of the entire dataset. Unlike groupby, they do not reduce rows they return the same number of rows with new calculated values.

Why We Need Window Functions?

✔ Trend analysis
✔ Moving averages
✔ Cumulative totals
✔ Time-series analysis
✔ Business analytics (sales growth, rolling stats)

Sample DataFrame

```

import pandas as pd

df = pd.DataFrame({

'Day': [1, 2, 3, 4, 5],

'Sales': [100, 200, 300, 400, 500]

})

print(df)

```

Rolling Window Function

rolling(window_size) applies a function on fixed-size moving window.

Practical: Rolling Mean (window = 2)

```

df['Rolling_Mean'] = df['Sales'].rolling(2).mean()

print(df)

```

Explanation

Rows used

Calculation

100

Not enough values → NaN

100, 200

(100+200)/2 = 150

200, 300

250

300, 400

350

400, 500

450

Expanding Window Function

expanding() applies a function from start → current row.

Practical: Cumulative Sum

```

df['Cumulative_Sum'] = df['Sales'].expanding().sum()

print(df)

```

Explanation

Rows Included

Sum

100

100

100+200

300

100+200+300

600

100+200+300+400

1000

All

1500

Common Window Functions

Function

Purpose

rolling()

Moving window

expanding()

Cumulative window

mean()

Average

sum()

Total

min()

Minimum

max()

Maximum

std()

Standard deviation

Example: Rolling Sum

```

df['Rolling_Sum'] = df['Sales'].rolling(3).sum()

print(df)

```

Difference: GroupBy vs Window Functions

Feature

GroupBy

Window Function

Rows

Reduced

Same

Output size

Smaller

Same as input

Use case

Aggregation

Trend analysis

Types

Function

Meaning

rolling

Fixed window

expanding

Growing window

ewm

Exponential

Chained Indexing Issues

Chained indexing happens when we access data using multiple indexing steps, which may create a copy instead of a view. This causes SettingWithCopyWarning and unreliable updates. Using .loc[] avoids this issue.

Sample DataFrame

```

import pandas as pd

df = pd.DataFrame({

'Name': ['A', 'B', 'C', 'D'],

'Sales': [80, 120, 90, 150]

})

print(df)

```

PROBLEM: Chained Indexing(wrong way)

```

df[df['Sales'] > 100]['Sales'] = 999

print(df)

```

Output (NO CHANGE): Sales values are NOT updated

Why This Happens?

```

Hide

df[df['Sales'] > 100] # creates a TEMPORARY COPY

['Sales'] # modifies the COPY, not original df

```

Pandas does not know whether you want to modify the original DataFrame or the copy.

CORRECT WAY: Use .loc[]

```

df.loc[df['Sales'] > 100, 'Sales'] = 999

print(df)

```

✔ Changes applied correctly
✔ No warning
✔ Safe & recommended

Therefore:

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges