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).
- Pandas is built on top of NumPy
- Used in Data Science, ML, AI, Analytics
Real-life example
Excel sheet → rows & columns → Pandas DataFrame
Importing Pandas
```
import pandas as pd
```
- pd is alias (short name)
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:
- Values
- Index
```
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:
- int64
- float64
- object (string)
- bool
```
X=df.dtypes
print(X)
```
Change dtype:
```
X=df["Age"] = df["Age"].astype(float)
print(X)
```
Index & Columns
- Index: Row labels
- Columns :Column names
```
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
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges