> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Pandas

# What is Pandas?

Pandas is a Python library used for:

* Data analysis
* Data cleaning
* Handling tables and CSV files
* Working with structured data easily

Main data structures:

* **Series** → 1D data
* **DataFrame** → 2D tabular data

***

# Installation

## Install Pandas

```bash theme={null}
pip install pandas
```

***

# Importing Pandas

```python theme={null}
import pandas as pd
```

`pd` is an alias used for shorter syntax.

***

# Series in Pandas

## `pd.Series()`

Creates a one-dimensional labeled array.

```python theme={null}
s = pd.Series([10,20,30,40,50])

s
```

### Output

```python theme={null}
0    10
1    20
2    30
3    40
4    50
dtype: int64
```

### Explanation

* Left side → index
* Right side → values
* `dtype` → datatype of values

***

# Series Attributes

## `.dtype`

Returns datatype of Series values.

```python theme={null}
s.dtype
```

### Output

```python theme={null}
dtype('int64')
```

### Explanation

All values are integers, so datatype is `int64`.

***

## `.values`

Returns all values as NumPy array.

```python theme={null}
s.values
```

### Output

```python theme={null}
array([10, 20, 30, 40, 50])
```

### Explanation

Converts Series values into NumPy array format.

***

## `.index`

Returns indexes of the Series.

```python theme={null}
s.index
```

### Output

```python theme={null}
RangeIndex(start=0, stop=5, step=1)
```

### Explanation

Indexes start from 0 and end at 4.

***

## `.name`

Assigns a name to the Series.

```python theme={null}
s.name = "number"

s
```

### Output

```python theme={null}
0    10
1    20
2    30
3    40
4    50
Name: number, dtype: int64
```

### Explanation

The Series now has label `"number"`.

***

# Indexing in Pandas Series

## Access Single Value

```python theme={null}
s[0]
```

### Output

```python theme={null}
10
```

### Explanation

Gets value present at index `0`.

***

## Slicing

```python theme={null}
s[0:2]
```

### Output

```python theme={null}
0    10
1    20
dtype: int64
```

### Explanation

Returns values from index `0` to `1`.\
End index is excluded.

***

# `iloc` → Position Based Indexing

Uses numeric positions.

## Single Position

```python theme={null}
s.iloc[3]
```

### Output

```python theme={null}
40
```

### Explanation

Returns value at position `3`.

***

## Multiple Positions

```python theme={null}
s.iloc[[1,2,3]]
```

### Output

```python theme={null}
1    20
2    30
3    40
dtype: int64
```

### Explanation

Fetches multiple positions together.

***

# Custom Index

```python theme={null}
index = ["apple","banana","grapes","orange","guava"]

s.index = index

s
```

### Output

```python theme={null}
apple     10
banana    20
grapes    30
orange    40
guava     50
Name: number, dtype: int64
```

### Explanation

Numeric indexes replaced with custom labels.

***

# Label Based Access

```python theme={null}
s['apple']
```

### Output

```python theme={null}
10
```

### Explanation

Returns value associated with label `"apple"`.

***

# `loc` → Label Based Indexing

Includes both start and end labels.

```python theme={null}
s['banana':'orange']
```

### Output

```python theme={null}
banana    20
grapes    30
orange    40
Name: calories, dtype: int64
```

### Explanation

Returns values from `"banana"` to `"orange"` inclusive.

***

# Creating Series from Dictionary

```python theme={null}
fruit_protein = {
    "apple":10,
    "banana":20,
    "grapes":30,
    "orange":40,
    "guava":50
}

s2 = pd.Series(fruit_protein)

s2
```

### Output

```python theme={null}
apple     10
banana    20
grapes    30
orange    40
guava     50
dtype: int64
```

### Explanation

Dictionary keys become indexes and values become Series values.

***

# Conditional Indexing

## Filtering Values

```python theme={null}
s2[s2 > 20]
```

### Output

```python theme={null}
grapes    30
orange    40
guava     50
dtype: int64
```

### Explanation

Returns only values greater than `20`.

***

# Logical Operators

## AND `&`

```python theme={null}
s2[(s2>30) & (s2<50)]
```

### Output

```python theme={null}
orange    40
dtype: int64
```

### Explanation

Both conditions must be true.

***

## OR `|`

```python theme={null}
s2[(s2>30) | (s2<50)]
```

### Output

```python theme={null}
apple     10
banana    20
grapes    30
orange    40
guava     50
dtype: int64
```

### Explanation

At least one condition should be true.

***

## NOT `~`

```python theme={null}
s2[~(s2>10)]
```

### Output

```python theme={null}
apple    10
dtype: int64
```

### Explanation

Reverses the condition.

***

# Modifying Series

```python theme={null}
s2['apple'] = 100

s2
```

### Output

```python theme={null}
apple     100
banana     20
grapes     30
orange     40
guava      50
dtype: int64
```

### Explanation

Updates value of `"apple"`.

***

# DataFrame in Pandas

## `pd.DataFrame()`

Creates table-like data.

```python theme={null}
df = pd.DataFrame(data)

df
```

### Output

```python theme={null}
   Name  Age  Salary Department
0  John   25   50000         IT
1  Jane   30   60000         HR
2  Jack   35   70000    Finance
3  Jill   40   80000  Marketing
```

### Explanation

Rows and columns together form a DataFrame.

***

# `head()`

Returns first rows.

```python theme={null}
df.head(2)
```

### Output

```python theme={null}
   Name  Age  Salary Department
0  John   25   50000         IT
1  Jane   30   60000         HR
```

### Explanation

Useful for previewing dataset.

***

# `tail()`

Returns last rows.

```python theme={null}
df.tail()
```

### Explanation

Shows ending rows of dataset.

***

# `iloc` in DataFrame

```python theme={null}
df.iloc[1:3]
```

### Output

```python theme={null}
   Name  Age  Salary Department
1  Jane   30   60000         HR
2  Jack   35   70000    Finance
```

### Explanation

Selects rows using positions.

***

# `loc` in DataFrame

```python theme={null}
df.loc[1:3, ["Name","Age"]]
```

### Output

```python theme={null}
   Name  Age
1  Jane   30
2  Jack   35
3  Jill   40
```

### Explanation

Selects rows and specific columns using labels.

***

# `drop()`

Removes rows or columns.

```python theme={null}
df.drop("Age", axis=1)
```

### Explanation

* `axis=1` → column
* `axis=0` → row

***

# `.shape`

Returns dataset dimensions.

```python theme={null}
df.shape
```

### Output

```python theme={null}
(4, 4)
```

### Explanation

4 rows and 4 columns.

***

# `info()`

Shows dataset summary.

```python theme={null}
df.info()
```

### Explanation

Displays:

* columns
* datatype
* null values
* memory usage

***

# `describe()`

Shows statistical summary.

```python theme={null}
df.describe()
```

### Explanation

Provides:

* mean
* std
* min
* max
* quartiles

***

# Broadcasting

Applies operation to entire column.

```python theme={null}
df["Salary"] = df["Salary"] + 10000
```

### Explanation

Adds `10000` to every salary value.

***

# `rename()`

Renames column names.

```python theme={null}
df.rename(columns={"Name":"Employee Name"}, inplace=True)
```

### Explanation

Changes `"Name"` column to `"Employee Name"`.

***

# `unique()`

Returns unique values.

```python theme={null}
df["Department"].unique()
```

### Output

```python theme={null}
['IT' 'HR' 'Finance' 'Marketing']
```

### Explanation

Removes duplicates and shows distinct values.

***

# `value_counts()`

Counts occurrences of values.

```python theme={null}
df["Department"].value_counts()
```

### Explanation

Counts frequency of each department.

***

# Missing Values

# `isnull()`

Checks missing values.

```python theme={null}
df1.isnull()
```

***

# `isnull().sum()`

Counts missing values column-wise.

```python theme={null}
df1.isnull().sum()
```

***

# `dropna()`

Removes missing values.

```python theme={null}
df1.dropna()
```

### Explanation

Deletes rows containing null values.

***

# `fillna()`

Fills missing values.

```python theme={null}
df1.fillna(0)
```

### Explanation

Replaces null values with `0`.

***

# Fill Missing Values with Mean

```python theme={null}
df1['Age'].fillna(df1['Age'].mean())
```

### Explanation

Uses average age to replace null.

***

# Forward Fill

```python theme={null}
df1['Age'].fillna(method='ffill')
```

### Explanation

Uses previous row value.

***

# Backward Fill

```python theme={null}
df1['Age'].fillna(method='bfill')
```

### Explanation

Uses next row value.

***

# `replace()`

Replaces specific values.

```python theme={null}
df1["Name"].replace('Jack','Tharun')
```

### Explanation

Changes `"Jack"` to `"Tharun"`.

***

# Duplicates

## `duplicated()`

Finds duplicate rows.

```python theme={null}
df[df.duplicated()]
```

***

# Lambda Functions

## `apply()`

Applies function to every value.

```python theme={null}
df["Age"] = df["Age"].apply(lambda x: x/2)
```

### Explanation

Divides every age by 2.

***

# Concatenation

## `concat()`

Combines DataFrames.

```python theme={null}
pd.concat([df,df_department])
```

### Explanation

Stacks DataFrames vertically.

***

# Merge / Join

## `merge()`

Joins DataFrames using common column.

```python theme={null}
pd.merge(df,df_department,on="Department")
```

### Explanation

Combines matching department rows together.

***

# Reading CSV Files

## `read_csv()`

Reads CSV files into DataFrame.

```python theme={null}
data = pd.read_csv("/content/house-prices.csv")
```

***

# Dataset Summary

## `data.info()`

Shows column information.

```python theme={null}
data.info()
```

***

# Statistical Summary

## `data.describe()`

Shows statistical details.

```python theme={null}
data.describe()
```
