The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Create a DataFramedf = pd.DataFrame({'name': ['Ada', 'Lin'], 'score': [91, 84]})View examples
Read CSV datadf = pd.read_csv('scores.csv', usecols=['name', 'score'])View examples
Preview rowspreview = df.head(3)View examples
Inspect column typestypes = df.dtypesView examples
Select one columnscores = df['score']View examples
Select by labelsresult = df.loc[df.index[:2], ['name', 'score']]View examples
Select by positionresult = df.iloc[:2, [0, 2]]View examples
Filter with a maskhigh_scores = df.loc[df['score'].ge(90)]View examples
Match allowed valuesselected = df.loc[df['team'].isin({'red', 'blue'})]View examples
Query rowsselected = df.query('score >= @minimum and active')View examples
Update matching rowsdf.loc[df['score'].ge(90), 'level'] = 'advanced'View examples
Create a derived columnresult = df.assign(percent=lambda frame: frame['score'] / frame['possible'] * 100)View examples
Rename columnsresult = df.rename(columns={'score': 'points'})View examples
Sort rowsresult = df.sort_values(['team', 'score'], ascending=[True, False])View examples
Set an indexindexed = df.set_index('student_id', verify_integrity=True)View examples
Restore a column indexflat = indexed.reset_index()View examples

A DataFrame aligns labeled columns and rows. Select by labels with loc, by integer positions with iloc, and perform assignment in one operation so the intended object is updated under pandas Copy-on-Write semantics.

Step by step

Detailed examples

01

Create a table and inspect its contract

DataFrame construction requires column values that can align to a common index. When reading external data, constrain columns and parsing options at the boundary, then inspect shape, labels, and dtypes before transforming values. A small head preview is useful, but it does not prove that the whole dataset follows the inferred schema.

Load selected CSV columns and inspect them
from io import StringIO
import pandas as pd

csv = StringIO('name,team,score,ignored\nAda,red,91,x\nLin,blue,84,y\nSam,red,96,z\n')
df = pd.read_csv(csv, usecols=['name', 'team', 'score'])

print(df.head(3).to_string(index=False))
print(df.dtypes.astype(str).to_dict())
Output
name team  score
 Ada  red     91
 Lin blue     84
 Sam  red     96
{'name': 'str', 'team': 'str', 'score': 'int64'}

Note: String dtype display depends on the pandas version and configured dtype backend; validate semantic types rather than relying only on printed dtype names.

Back to quick reference ↑
02

Select with labels or integer positions

Bracket selection returns a Series for one column. loc interprets both axes as labels and includes both ends of a label slice, while iloc uses Python-style integer positions and excludes the slice stop. Select both axes explicitly when downstream code depends on a stable table shape.

Compare column, label, and position selection
import pandas as pd

df = pd.DataFrame(
    {'name': ['Ada', 'Lin', 'Sam'], 'team': ['red', 'blue', 'red'], 'score': [91, 84, 96]},
    index=['a1', 'l2', 's3'],
)

scores = df['score']
by_label = df.loc[['a1', 'l2'], ['name', 'score']]
by_position = df.iloc[:2, [0, 2]]

print(scores.to_dict())
print(by_label.equals(by_position.set_axis(by_label.index)))
Output
{'a1': 91, 'l2': 84, 's3': 96}
True
Back to quick reference ↑
03

Build explicit boolean filters

Boolean masks align by index, so derive them from the same DataFrame unless deliberate alignment is part of the operation. Use parentheses around combined comparisons, isin for membership, and query for readable expressions over valid column names. Values injected into query with @ remain data, but query strings should still be developer-controlled rather than accepted as an authorization boundary.

Filter scores with masks, membership, and query
import pandas as pd

df = pd.DataFrame({
    'name': ['Ada', 'Lin', 'Sam', 'Mia'],
    'team': ['red', 'blue', 'green', 'red'],
    'score': [91, 84, 96, 88],
    'active': [True, True, False, True],
})

high_scores = df.loc[df['score'].ge(90), ['name', 'score']]
selected_teams = df.loc[df['team'].isin({'red', 'blue'})]
minimum = 85
active_scores = df.query('score >= @minimum and active')

print(high_scores['name'].tolist())
print(selected_teams['name'].tolist())
print(active_scores['name'].tolist())
Output
['Ada', 'Sam']
['Ada', 'Lin', 'Mia']
['Ada', 'Mia']
Back to quick reference ↑
04

Assign without chained indexing

Use one loc assignment to target the original DataFrame. Chained assignment cannot update the parent object under pandas 3 Copy-on-Write and is ambiguous in earlier releases. assign is useful in expression pipelines because it returns a new DataFrame and evaluates callable columns against the DataFrame produced so far.

Update selected rows and derive percentages
import pandas as pd

df = pd.DataFrame({
    'name': ['Ada', 'Lin', 'Sam'],
    'score': [91, 42, 96],
    'possible': [100, 50, 120],
})

df.loc[df['score'].ge(90), 'level'] = 'advanced'
result = df.assign(percent=lambda frame: frame['score'] / frame['possible'] * 100)

print(result[['name', 'level', 'percent']].to_string(index=False))
Output
name    level  percent
 Ada advanced     91.0
 Lin      NaN     84.0
 Sam advanced     80.0

Note: Do not write df[df['score'] >= 90]['level'] = 'advanced'; that chained operation does not reliably update df.

Back to quick reference ↑
05

Make labels and ordering intentional

rename changes labels without touching values. sort_values provides deterministic ordering when all relevant tie-breakers are included. set_index makes a domain key available for label-based alignment; verify_integrity checks uniqueness immediately but has a cost, so use it where duplicate identifiers indicate invalid data. reset_index restores index levels as columns.

Rename, sort, index, and flatten records
import pandas as pd

df = pd.DataFrame({
    'student_id': [102, 101, 103],
    'team': ['red', 'blue', 'red'],
    'score': [91, 84, 96],
})

renamed = df.rename(columns={'score': 'points'})
ordered = renamed.sort_values(['team', 'points'], ascending=[True, False])
indexed = ordered.set_index('student_id', verify_integrity=True)
flat = indexed.reset_index()

print(flat.to_dict('records'))
Output
[{'student_id': 101, 'team': 'blue', 'points': 84}, {'student_id': 103, 'team': 'red', 'points': 96}, {'student_id': 102, 'team': 'red', 'points': 91}]
Back to quick reference ↑

Local code tester

Filter and update a DataFrame

Change the scores or threshold and run a Copy-on-Write-safe pandas selection locally.

Runs in your browser
Output
Press Run to load Python locally.

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. pandas development team10 minutes to pandaspandas.pydata.org
  2. pandas development teamIndexing and selecting datapandas.pydata.org
  3. pandas development teamCopy-on-Write (CoW)pandas.pydata.org
  4. pandas development teampandas.DataFramepandas.pydata.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback