Selecting and Filtering Data in Pandas
Most of the time, you only need part of your data. This topic teaches you how to pick specific rows and columns, filter with conditions, and sort your results.
a. Selecting Rows and Columns
To pick one column, use its name inside square brackets. To pick several columns, pass a list of names. The result is a smaller DataFrame.
print(df["name"])
print(df[["name", "city"]])To pick rows, you can use slicing. For example, df[0:3] returns the first three rows. This works like slicing a list.
You can also use df.head(10) to see the top ten rows or df.tail(10) to see the last ten. These quick methods are helpful when checking a large dataset.
b. Using loc and iloc
loc selects data by labels, which means row and column names. iloc selects data by position, which means row and column numbers. Knowing the difference is very important.
print(df.loc[0, "name"])
print(df.iloc[0, 1])With loc, you can pick rows and columns by name, like df.loc[0:2, ["name", "age"]]. Note that loc includes the end label in the range.
With iloc, the end position is not included, just like normal Python slicing. A simple way to remember: loc is for labels and iloc is for index positions.
c. Filtering with Conditions
Filtering means keeping only the rows that match a condition. You write the condition inside square brackets. Pandas returns only the rows where the condition is True.
adults = df[df["age"] > 25]
chennai = df[df["city"] == "Chennai"]You can combine conditions using & for and, and | for or. Always wrap each condition in round brackets.
result = df[(df["age"] > 25) & (df["city"] == "Delhi")]The isin() method is handy when you want to match several values at once. For example, df[df["city"].isin(["Delhi", "Mumbai"])] returns rows from either city.
d. Sorting Data
Sorting helps you see the highest or lowest values quickly. Use sort_values() and tell Pandas which column to sort by.
df_sorted = df.sort_values("age", ascending=False)By default, the sort goes from smallest to largest. Set ascending=False to reverse it. You can also sort by more than one column by passing a list.
Use sort_index() to sort by the row labels instead. Combine sorting with head() to get a quick top five list, like the five oldest customers or the five highest sales.
Data Analysis with Python Handbook
Wesline Rinisha








