Getting Started with Pandas
Pandas is a powerful Python library for data manipulation and analysis. Pandas is your gateway to managing data effectively. First step: bring it to your project.
It’s like Excel, but smarter and much less likely to crash when you sort something.
import pandas as pd
Pandas Series
A Pandas Series is a one-dimensional labeled array. Think of it as a fancy list with labels (indices). We can access values by their indices.
Example 1: Create a basic Series from the List.
s = pd.Series([10, 20, 30])
print(s)
Example 2: Assign custom indices to the elements.
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s)
Example 3: Create a Series from the dictionary.
s = pd.Series({"bmw": 10, "honda": 9, "suzuki": 8})
print(s)
Example 4: Create a Series by selecting specific keys from the dictionary.
s = pd.Series({'bmw': 10, 'honda': 9, 'suzuki': 8}, index=['bmw', 'honda'])
print(s)
💡
Fun fact: A Series can hold any data type. Yes, even your favorite memes. In those cases, the dtype will be an object.
Pandas DataFrame
A DataFrame is a 2D-labeled data structure. Think of it as rows and columns. It’s the bread and butter of Pandas.
Example 1: Create a DataFrame from a dictionary.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
print(df)
Example 2: Create a DataFrame from lists.
data = [[1, "John"], [2, "Bob"]]
df = pd.DataFrame(data, columns=["ID", "Name"])
print(df)
💡
Pro tip: DataFrames can hold multiple data types, just like that one junk drawer in your kitchen.
Pandas Index
The Index is a label array of stored rows and columns. Labels can also be customized.
Example 1: Setting a column as an Index.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
df.set_index("Name", inplace=True)
print(df)
Example 2: Setting a range as an Index.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data, index=pd.RangeIndex(100, 102, name="Index"))
print(df)
Example 3: Renaming an Index.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data, index=pd.RangeIndex(100, 102, name="Index"))
df.rename(index={101: "First"}, inplace=True)
print(df)
Example 4: Resetting an Index.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
df.set_index("Name", inplace=True)
df.reset_index(inplace=True)
print(df)
Example 5: Listing Indices.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
print(df.index)
print(df.index.values)
💡
Remember, the index is your friend, use it wisely. Few more types of indexes are CategoricalIndex, DatetimeIndex, IntervalIndex, etc.
Pandas Array
Pandas integrates with NumPy arrays for efficient data storage.
Example 1: Create a pandas array and pandas Series.
arr = pd.array(["John", "Alice", "Bob"])
arr_series = pd.Series(arr)
print(arr_series)
Example 2: Convert a column to a NumPy array.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
arr = df["Name"].to_numpy()
print(arr)
Example 3: Modify data using NumPy.
data = {
"Name": ["Alice", "Bob"],
"Age": [25, 30],
}
df = pd.DataFrame(data)
arr = df["Age"].to_numpy()
df["Age"] = arr + 2
print(df)
💡
Fun fact: If you’re a NumPy fan, you’ll feel right at home here.