Pandas: DataFrame Operation

pandas
dataframe
Understanding various DataFrame options provided by the pandas lib
Author

Mohammed Adil Siraju

Published

September 16, 2025

This notebook covers essential operations on pandas DataFrames: reading, viewing, selecting, filtering, and indexing.

Reading Data

Load data from files like CSV into DataFrames using pd.read_csv().

import pandas as pd

df = pd.read_csv('example.csv')
df
Name Age City
0 Adil 23 Matannur
1 Aman 19 Vellore
2 Ziya 15 Tly
3 Zahra 9 Knr

Viewing Data

Use head() and tail() to preview the first/last rows of your DataFrame.

df.head(2)
Name Age City
0 Adil 23 Matannur
1 Aman 19 Vellore
df.tail(2)
Name Age City
2 Ziya 15 Tly
3 Zahra 9 Knr

Selecting Columns

Select specific columns using bracket notation or .loc.

df[['Name', 'City']]
Name City
0 Adil Matannur
1 Aman Vellore
2 Ziya Tly
3 Zahra Knr
df[['Name', 'City']].values
array([['Adil', 'Matannur'],
       ['Aman', 'Vellore'],
       ['Ziya', 'Tly'],
       ['Zahra', 'Knr']], dtype=object)

Filtering Data

Filter rows based on conditions using boolean indexing.

agegt20 = df[df['Age']>=20]
agegt20
Name Age City
0 Adil 23 Matannur
agelt20 = df[df['Age']<20]
agelt20
Name Age City
1 Aman 19 Vellore
2 Ziya 15 Tly
3 Zahra 9 Knr
df_ziya = df[df['Name']=='Ziya']
df_ziya
Name Age City
2 Ziya 15 Tly
df_mult_cond = df[(df['City']=='Matannur') | (df['Age']<=15 )]
df_mult_cond
Name Age City
0 Adil 23 Matannur
2 Ziya 15 Tly
3 Zahra 9 Knr
selected_cities = ['Tly', 'Knr']
df_tlyorknr = df[df['City'].isin(selected_cities)]

df_tlyorknr
Name Age City
2 Ziya 15 Tly
3 Zahra 9 Knr
df[df['Name'].str.startswith('Z')]
Name Age City
2 Ziya 15 Tly
3 Zahra 9 Knr

Indexing and Slicing

Use .iloc for position-based and .loc for label-based indexing.

df.iloc[0:2]
Name Age City
0 Adil 23 Matannur
1 Aman 19 Vellore
df.loc[df['Age']>20, ['Name', 'City']]
Name City
0 Adil Matannur

Best Practices

  • Use .copy() when assigning filtered DataFrames to avoid modifying originals.
  • Combine conditions with & and | for complex filters.
  • Prefer .loc for clarity in production code.

Summary

This notebook covered key DataFrame operations: reading, viewing, selecting, filtering, and indexing. These form the foundation for data manipulation!