Code & computing / QUICK REFERENCE
pandas DataFrame tasks: select, clean and aggregate
Everyday pandas DataFrame recipes for row filtering, missing values, grouping, joins and reshaping.
Select rows and columns
- Choose one column
df['sales']Returns a Series containing that named column. [1]
- Choose several columns
df[['city','sales']]Returns a DataFrame with selected columns in that order. [1]
- First five rows
df.head(5)View the leading rows without printing all records. [1]
- Last five rows
df.tail(5)Preview recent rows when source order is meaningful. [1]
- Row by position
df.iloc[0]Gets the first positional row as a Series. [1]
- Rows by label
df.loc[3:7]Label slicing includes both endpoints if labels exist. [1]
- Slice first ten rows
df.iloc[:10]Uses positional slicing with an excluded ending offset. [1]
- Filter numeric threshold
df.loc[df['sales']>100]Boolean mask keeps rows whose sales exceed 100. [1]
- Combine two conditions
df.loc[(df['sales']>100)&(df['city']=='NY')]Parenthesize each predicate before the & operator. [1]
- Choose matching cities
df.loc[df['city'].isin(['NY','LA'])]Match values against a set of allowed names. [1]
- Exclude rows
df.loc[~df['status'].eq('done')]Negate the boolean mask with tilde. [1]
- Query a filter
df.query('sales > 100')Uses a string expression with pandas query syntax. [1]
Clean types and missing data
- Drop only missing key
df.dropna(subset=['id'])Requires the id field but keeps other incomplete values. [2][1]
- Fill numeric null
df['sales'].fillna(0)Produces a filled Series without modifying the source unless assigned. [2][1]
- Fill different columns
df.fillna({'sales':0,'city':'Unknown'})- Convert to numeric
pd.to_numeric(df['sales'],errors='coerce')- Convert date strings
pd.to_datetime(df['date'],errors='coerce')
Group and summarize
- Sum by city
df.groupby('city')['sales'].sum()Returns total sales for each nonmissing city. [3]
- Average by city
df.groupby('city')['sales'].mean()Group mean for numeric sales. [3]
- Count rows per city
df.groupby('city').size()Counts all rows in each nonmissing city group. [3]
- Count nonmissing values
df.groupby('city')['sales'].count()Unlike size, excludes missing sales in count. [3]
- Group multiple keys
df.groupby(['city','status'])['sales'].sum()Aggregates by combinations of two keys. [3]
- Named aggregates
df.groupby('city').agg(total=('sales','sum'),n=('id','count'))Produces renamed aggregate columns with explicit operations. [3]
- Keep group labels as columns
df.groupby('city',as_index=False)['sales'].sum()Returns the city group as a column instead of index. [3]
- Cumulative total
df.groupby('city')['sales'].cumsum()Running sum per city in existing row order. [3]
- Share of group total
df['sales']/df.groupby('city')['sales'].transform('sum')Transform returns a row-aligned group sum. [3]
- Median by group
df.groupby('city')['sales'].median()Robust central tendency for each group. [3]
- Group with missing keys
df.groupby('city',dropna=False)['sales'].sum()Include missing city values as a separate group. [3]
- Sort group totals
df.groupby('city')['sales'].sum().sort_values(ascending=False)Highest totals appear first. [3]
Join and reshape
- Left join on ID
df.merge(other,on='id',how='left')- Validate one-to-one join
df.merge(other,on='id',validate='one_to_one')- Flag unmatched joins
df.merge(other,on='id',how='left',indicator=True)- Join different keys
df.merge(other,left_on='id',right_on='customer_id')- Concatenate rows
pd.concat([df,other],ignore_index=True)- Concatenate columns
pd.concat([df,other],axis=1)Aligns columns by index labels, not physical position. [4][1]
- Pivot categories to columns
df.pivot(index='id',columns='status',values='sales')- Aggregate while pivoting
df.pivot_table(index='city',columns='status',values='sales',aggfunc='sum')Combines duplicate category pairs through aggregation. [4][1]
- Long format
df.melt(id_vars=['id'],var_name='measure',value_name='reading')
Reserved placement, separate from reference controls. No ad network is active.
No matching entry. Try a shorter word or clear the search to see the complete sheet.
Sources
cheatsheetatlas.com/sheets/pandas/ · Updated 2026-10-20 · pandas 3.x · Python