Your First DataFrame
Alright, let's create your first DataFrame. This is the moment everything clicks. Think of a DataFrame as a table — rows and columns, just like a spreadsheet or a database table.
Creating a DataFrame from a Dictionary
The easiest way to create a DataFrame is from a Python dictionary. Each key becomes a column name, and the values become the column data.
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Score': [88.5, 92.3, 79.8]
}
df = pd.DataFrame(data)
print(df)
Look at that output! Pandas automatically created row labels (0, 1, 2) and organized everything into a clean table. No more clicking through Excel cells.
Rows and Columns
Here is the thing about DataFrames — rows represent individual records (like a person), and columns represent attributes (like name or age). Each column is actually a Series, which is like a one-dimensional array.
One thing that confused me at first was the difference between a Series and a DataFrame. Think of it this way: a Series is one column, a DataFrame is the whole table. Simple as that.
Try it Yourself →Key Takeaways
- A DataFrame is a table with rows and columns
- Create one from a dictionary where keys are column names
- Pandas auto-generates row labels (the index)
- Each column in a DataFrame is a Series