DataFrames — Core Operations
The DataFrame is the most important object in Python data analytics. If you master DataFrames, you can do 90% of what Excel does — and 100x faster on large data.
How do you create a DataFrame from a dictionary?
import pandas as pd
df = pd.DataFrame({
"name": ["Rahul", "Priya", "Aman"],
"age": [28, 25, 32],
"salary": [60000, 75000, 90000]
})
df.shape # (3, 3)
df.columns # Index(["name", "age", "salary"])
df.dtypes # types of each columnEach dict key becomes a column. shape gives (rows, columns). Always check shape and dtypes immediately after loading data — it tells you if the import worked correctly.
How do you select columns and rows from a DataFrame?
df["name"] # single column → Series
df[["name","salary"]] # multiple columns → DataFrame
df.iloc[0] # first row by position
df.iloc[0:3] # first 3 rows
df.loc[0, "name"] # row 0, column "name"
df.loc[df["age"] > 27, ["name","salary"]] # filter + select.iloc for position-based, .loc for label-based. The most important pattern: df.loc[condition, columns] — filter rows and select columns in one step.
How do you add, rename, and drop columns?
# Add column:
df["bonus"] = df["salary"] * 0.10
df["grade"] = df["age"].apply(lambda x: "senior" if x > 30 else "junior")
# Rename:
df.rename(columns={"name": "full_name"}, inplace=True)
# Drop:
df.drop(columns=["bonus"], inplace=True)
df.drop(columns=["bonus"]) # returns new df, original unchangedinplace=True modifies the DataFrame in memory. Without it, the operation returns a new DataFrame. Best practice: avoid inplace and reassign — it is clearer and avoids bugs.
How do you filter rows based on conditions?
df[df["salary"] > 70000] # single condition
df[(df["salary"] > 70000) & (df["age"] < 30)] # AND
df[(df["salary"] > 90000) | (df["age"] < 26)] # OR
df[df["name"].isin(["Rahul", "Aman"])] # in list
df[df["name"].str.contains("ra", case=False)] # text filterNever use Python and/or in Pandas conditions — use & and |. Each condition in parentheses. This is one of the most common beginner mistakes that causes TypeErrors.
How do you get summary statistics of a DataFrame?
df.describe() # count, mean, std, min, max, quartiles
df.info() # column types, non-null counts, memory
df.head(5) # first 5 rows
df.tail(5) # last 5 rows
df.sample(3) # 3 random rows
df.shape # (rows, cols)
df.isnull().sum() # null count per columnThis is your EDA starting sequence. Run all of these on any new dataset before touching a single column. info() reveals hidden issues — dtypes that should be numeric showing as object, for example.
EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS
Want to master Python with live practice?
Join our Python for Data Analysis course — live classes in Noida and online across India.
Book Free Demo Class →