0% found this document useful (0 votes)
10 views1 page

Pandas DataFrame Indexing & Filtering

Uploaded by

deepshree sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Pandas DataFrame Indexing & Filtering

Uploaded by

deepshree sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pandas: Reindex, Dropping, Indexing, Selection, and Filtering

1. Reindex Function Arguments

The

reindex()

function in Pandas is used to conform a DataFrame to a new index or columns, introducing

NaN

values where data is missing.

Common Arguments:

index: A new index to conform to.


columns: A new set of columns to conform to.
fill_value: Value used to fill missing values (default is NaN).
method: Filling method (

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'])

new_df = [Link](index=['x', 'y', 'z', 'w'], columns=['A', 'B', 'C'], f


print(new_df)

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

2. Dropping Entries from an Axis

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)

Output (Dropping Row):

A B
x 10.0 40.0
z 30.0 60.0

Output (Dropping Column):

A
x 10.0
y 20.0
z 30.0

3. Indexing in Pandas

Pandas provides label-based indexing with

loc[]

and position-based indexing with

iloc[]

Example:

# Using loc (label-based)


loc_example = [Link]['x']
print(loc_example)

# Using iloc (position-based)


iloc_example = [Link][1]
print(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)

# Selecting multiple columns


multi_column_selection = df[['A', 'B']]
print(multi_column_selection)

# Conditional row selection


conditional_selection = df[df['A'] > 15]
print(conditional_selection)

Output (Single Column):

x 10
y 20
z 30
Name: A, dtype: int64

Output (Multiple Columns):

A B
x 10.0 40.0
y 20.0 50.0
z 30.0 60.0

Output (Conditional Selection):

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:

# Filter rows where 'A' is greater than 15


filtered_df = df[df['A'] > 15]
print(filtered_df)

# Filter with multiple conditions


filtered_multi_cond = df[(df['A'] > 15) & (df['B'] < 60)]
print(filtered_multi_cond)

Output (Single Condition Filtering):

A B
y 20.0 50.0
z 30.0 60.0

Output (Multiple Condition Filtering):

A B
y 20.0 50.0

© 2024 Pandas Examples

You might also like