Introduction to Pandas

Python tutorial · PySpark.in

Introduction to Pandas

Pandas is a Python library used for data analysis and data manipulation. It helps us work with structured data like tables (rows & columns).

Real-life example

Excel sheet → rows & columns → Pandas DataFrame

Importing Pandas

```

import pandas as pd

```

Pandas vs NumPy

Feature

NumPy

Pandas

Data Type

Arrays

Series, DataFrame

Labels

No

Yes

Missing Data

Weak

Strong

Use Case

Numerical computing

Data analysis

NumPy = numbers
Pandas = real-world data

Series

A Series is a 1D labeled array.
It has:

```

import pandas as pd

s = pd.Series([10, 20, 30, 40])

print(s)

s = pd.Series([10, 20, 30], index=['a','b','c'])

print(s)

```

DataFrame

A DataFrame is a 2D table (rows + columns). It is the most important Pandas structure.

```

data = {

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

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

"Marks": [85, 90, 88]

}

df = pd.DataFrame(data)

print(df)

```

Creating Series & DataFrames

From List

```

X=pd.Series([1,2,3])

print(X)

```

From Dictionary

```

Y=pd.DataFrame({

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

"Population": [20, 25]

})

print(Y)

```

From NumPy Array

```

import numpy as np

arr = np.array([10,20,30])

pd.Series(arr)

print(arr)

```

Data Types (dtypes)

Each column has a data type:

```

X=df.dtypes

print(X)

```

Change dtype:

```

X=df["Age"] = df["Age"].astype(float)

print(X)

```

Index & Columns

```

X=df.index

Y=df.columns

print(X)

print(Y)

```

Rename columns:

```

df.columns = ["Student_Name", "Student_Age", "Student_Marks"]

print(df.columns)

```

Reading Data (CSV, Excel, JSON)

Pandas can read data from files.

CSV

```

df = pd.read_csv("data.csv")

```

Excel

```

df = pd.read_excel("data.xlsx")

```

JSON

```

df = pd.read_json("data.json")

```

Writing Data to Files

```

df.to_csv("output.csv", index=False)

df.to_excel("output.xlsx", index=False)

df.to_json("output.json")

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges