# Python Pandas Module

Pandas is a fast, powerful, flexible, and easy-to-use open source data analysis and manipulation tool, built on top of the Python programming language.

## Installation

```bash
pip install pandas
```

## Importing the Module

```python
import pandas as pd
```

## Data Structures

### Series

A one-dimensional labeled array capable of holding any data type.

```python
s = pd.Series([1, 3, 5, 6, 8])
print(s)
```

### DataFrame

A two-dimensional, size-mutable, potentially heterogeneous tabular data structure.

```python
data = {
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["New York", "Paris", "London"]
}

df = pd.DataFrame(data)
print(df)
```

## Reading and Writing Data

Pandas supports reading from and writing to various file formats (CSV, Excel, SQL, JSON, etc.).

```python
# Reading
df = pd.read_csv("data.csv")
# df_excel = pd.read_excel("data.xlsx")

# Writing
df.to_csv("output.csv", index=False)
# df.to_excel("output.xlsx", index=False)
```

## Viewing Data

```python
print(df.head())      # First 5 rows
print(df.tail())      # Last 5 rows
print(df.info())      # Index, Datatype and Memory information
print(df.describe())  # Summary statistics for numerical columns
print(df.columns)     # Column names
```

## Selection

### By Column

```python
ages = df["Age"]
```

### By Label (`.loc`) and Position (`.iloc`)

```python
# Selects rows by label/index
row = df.loc[0] 

# Selects rows by integer position
first_row = df.iloc[0]
```

## Filtering

```python
# Rows where Age is greater than 28
adults = df[df["Age"] > 28]
```

## Handling Missing Data

```python
df.dropna()       # Drop rows with missing values
df.fillna(value=0) # Fill missing values with 0
```

## Grouping

```python
# Group by a column and calculate mean
# df.groupby("City").mean()
```

[[programming/python/python]]