A pandas DataFrame is a table, just like an R data frame or a spreadsheet. It is the central data structure for almost all real-world data analysis in Python.
Creating a DataFrame
R Code
import pandas as pd df = pd.DataFrame({ "name": ["Rafi", "Meera", "Zayn"], "age": [23, 25, 21], "score": [88, 92, 76] }) df
Output
name age score 0 Rafi 23 88 1 Meera 25 92 2 Zayn 21 76
Exploring a DataFrame
R Code
df.info() # column types and non-null counts print(df.shape) # (rows, columns) print(df.columns) df.head(2) # first 2 rows df.describe() # quick statistical summary of numeric columns
Output
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 3 columns): name 3 non-null object age 3 non-null int64 score 3 non-null int64 (3, 3) Index(['name', 'age', 'score'], dtype='object')
Selecting Columns
R Code
df["name"] # one column, as a Series df[["name", "score"]] # multiple columns, stays a DataFrame
Output
0 Rafi 1 Meera 2 Zayn Name: name, dtype: object
Selecting Rows & Filtering
R Code
df.iloc[0] # first row, by position df[df["score"] > 80] # rows where score > 80 df[(df["score"] > 80) & (df["age"] < 25)] # combine conditions with &
Output
name Rafi
age 23
score 88
Name: 0, dtype: object
name age score
0 Rafi 23 88
1 Meera 25 92⚠ Common Mistake
Use & and | (not and/or) when combining conditions inside DataFrame filtering, and wrap each condition in its own parentheses - (df.a > 1) & (df.b < 5). Python's and/or do not work element-wise on Series and will raise an error.Adding & Modifying Columns
R Code
df["passed"] = df["score"] >= 80 df["grade"] = df["score"].apply(lambda s: "A" if s >= 90 else "B") df
Output
name age score passed grade 0 Rafi 23 88 True B 1 Meera 25 92 True A 2 Zayn 21 76 False B
Sorting and Grouping
R Code
df.sort_values("score", ascending=False) # grouped summary, similar to R's aggregate() df.groupby("passed")["score"].mean()
Output
passed False 76.0 True 90.0 Name: score, dtype: float64
Advanced: Reading, Filtering, and Summarising in One Chain
🚀 Advanced Example
Pandas encourages chaining several operations together into one readable pipeline - a very common real-world pattern.R Code
result = (
df[df["age"] < 25]
.sort_values("score", ascending=False)
[["name", "score"]]
)
resultOutput
name score 0 Rafi 88 2 Zayn 76
Key Points
- A pandas DataFrame is Python\'s equivalent of an R data frame - a table where each column has its own type.
- Use df.info(), df.shape, and df.describe() to explore any new dataset first.
- Combine filter conditions with & and |, not and/or, and wrap each condition in parentheses.
- df.groupby(column)[value_column].mean() computes grouped summaries, similar to R\'s aggregate().