Pandas DataFrame Indexing & Filtering
Pandas DataFrame Indexing & Filtering
The
reindex()
NaN
Common Arguments:
ffill
bfill
).
limit: Maximum number of missing values to fill.
tolerance: Maximum distance for inexact matches.
Example:
import pandas as pd
df = [Link]({
'A': [10, 20, 30],
'B': [40, 50, 60]
}, index=['x', 'y', 'z'])
Output:
A B C
x 10.0 40.0 0
y 20.0 50.0 0
z 30.0 60.0 0
w 0.0 0.0 0
The
drop()
function is used to remove entries (rows or columns) from a DataFrame along a specified
axis (rows: axis=0, columns: axis=1).
Example:
# Dropping a row
df_drop_row = [Link]('y')
print(df_drop_row)
# Dropping a column
df_drop_col = [Link]('B', axis=1)
print(df_drop_col)
A B
x 10.0 40.0
z 30.0 60.0
A
x 10.0
y 20.0
z 30.0
3. Indexing in Pandas
loc[]
iloc[]
Example:
Output (loc):
A 10
B 40
Name: x, dtype: int64
Output (iloc):
A 20
B 50
Name: y, dtype: int64
4. Selection in Pandas
You can select specific rows or columns using labels or Boolean conditions.
Example:
# Selecting a column
column_selection = df['A']
print(column_selection)
x 10
y 20
z 30
Name: A, dtype: int64
A B
x 10.0 40.0
y 20.0 50.0
z 30.0 60.0
A B
y 20.0 50.0
z 30.0 60.0
5. Filtering in Pandas
Filtering allows you to select rows based on conditions applied to DataFrame columns.
Example:
A B
y 20.0 50.0
z 30.0 60.0
A B
y 20.0 50.0