About Lesson
To begin using Pandas, you first need to install it. Pandas can be installed using Python’s package manager, pip:
pip install pandas
After installation, import the library into your Python script or interactive environment:
import pandas as pd
The alias pd is a convention followed by most users for brevity.
Core Data Structures in Pandas
1. Series:
- A one-dimensional array with labels (indexes).
- Example:
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(data)
Output:
a 10
b 20
c 30
dtype: int64
2. DataFrame:
- A two-dimensional table with rows and columns, much like a spreadsheet.
- Example:
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df)
Output:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
3. Indexing:
- Pandas supports label-based and positional indexing.
- Example of accessing elements by index:
print(df['Name']) # Access a column
print(df.loc[0]) # Access a row by label
print(df.iloc[1]) # Access a row by position