Data Structures in Pandas
Pandas is built around two core data structures: the Series, a one-dimensional labeled array, and the DataFrame, a two-dimensional labeled table made up of multiple Series stacked together as columns.
The Series
A Series holds a single column of data, along with an index that labels each value - similar to a single column in a spreadsheet.
import pandas as pd
scores = pd.Series([85, 92, 78], index=["Abhay", "Priya", "Rohit"])
print(scores)
# Abhay 85
# Priya 92
# Rohit 78
# dtype: int64
print(scores["Priya"]) # 92 - access by label
The DataFrame
A DataFrame is a two-dimensional table made up of rows and columns, where each column is technically a Series. It's the primary structure you'll work with for almost all real data analysis in Pandas.
import pandas as pd
data = {
"Name": ["Abhay", "Priya", "Rohit"],
"Course": ["Data Science", "Data Science", "Data Science"],
"Score": [85, 92, 78]
}
df = pd.DataFrame(data)
print(df)
print(type(df["Score"])) # <class 'pandas.core.series.Series'> - each column is a Series
Creating a DataFrame from Other Sources
import pandas as pd
# From a list of lists
df2 = pd.DataFrame([[1, "A"], [2, "B"]], columns=["ID", "Grade"])
# From a list of dictionaries
df3 = pd.DataFrame([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
You've Completed This Section
You now understand the two building blocks of Pandas - the Series and the DataFrame - and how they work together to represent real-world tabular data. This sets you up perfectly for the hands-on data manipulation techniques covered next in the full course.