Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

This cheat sheet covers the most common Pandas DataFrame commands students use when working with data in Python. DataFrames are used to organize data into rows and columns, similar to a spreadsheet or database table. Students need this reference to quickly remember syntax for creating, viewing, selecting, cleaning, and summarizing data.

It is especially useful for data science, statistics, and computer science projects.

Key Facts

  • Create a DataFrame from a dictionary with df = pd.DataFrame({'name': ['Ana', 'Ben'], 'score': [92, 85]}).
  • View the first rows with df.head(n), where n is the number of rows to display, such as df.head(5).
  • Select one column with df['column_name'] and select multiple columns with df[['col1', 'col2']].
  • Filter rows with a condition using df[df['score'] >= 90], which returns only rows where the score is at least 90.
  • Use df.loc[row_label, column_label] for label-based selection and df.iloc[row_index, column_index] for position-based selection.
  • Create or replace a column with df['new_column'] = expression, such as df['passed'] = df['score'] >= 70.
  • Handle missing values with df.dropna() to remove missing rows or df.fillna(value) to replace missing entries.
  • Summarize groups with df.groupby('category')['value'].mean(), which calculates the mean value for each category.

Vocabulary

DataFrame
A two-dimensional Pandas data structure that stores data in labeled rows and columns.
Series
A one-dimensional Pandas data structure, usually representing one column from a DataFrame.
Index
The labels used to identify rows in a DataFrame or values in a Series.
Column
A named vertical set of values in a DataFrame, often representing one variable or feature.
Boolean mask
A Series of True and False values used to filter rows in a DataFrame.
GroupBy
A Pandas operation that splits data into groups so an aggregation can be applied to each group.

Common Mistakes to Avoid

  • Using df.column name when the column has spaces is wrong because dot notation only works for simple column names. Use df['column name'] instead.
  • Forgetting parentheses on methods is wrong because df.head shows the method object instead of running it. Use df.head() to display rows.
  • Using = instead of == inside a filter is wrong because = assigns a value and == checks equality. Write df[df['grade'] == 'A'] for a comparison.
  • Confusing loc and iloc is wrong because loc uses labels while iloc uses integer positions. Use df.loc[3, 'score'] for row label 3 and df.iloc[3, 1] for the fourth row and second column by position.
  • Changing a filtered copy without saving it is risky because the original DataFrame may not update. Assign the result back, such as df = df[df['score'] >= 70], when you want to keep the filtered data.

Practice Questions

  1. 1 Create a DataFrame named scores with columns student and score using the data Ana 92, Ben 85, and Cara 97.
  2. 2 Given df has a column score, write the Pandas command that returns only rows where score is greater than or equal to 90.
  3. 3 Given df has columns class and score, write the command to calculate the average score for each class.
  4. 4 Explain when you would use df.loc instead of df.iloc, and describe how their selection rules are different.

Understanding Pandas DataFrame Quick Reference

A DataFrame has more structure than a simple table. Each column usually represents one variable, such as test score or city. Each row represents one observation, such as one student or one day of weather.

Pandas keeps an index for the rows. The index may look like row numbers, but it can be names, dates, or IDs. This matters because labels and positions are different ideas.

A label identifies a row by its index value. A position identifies where the row appears from top to bottom. Confusing these can return the wrong data without causing an obvious error.

Most Pandas work uses whole columns at once. This is called vectorized work. Instead of writing a Python loop to examine every score, a comparison can create a full column of true and false values in one step.

That result is a Boolean mask. Pandas uses the mask to keep matching rows. This approach is usually clearer and faster than looping.

When combining conditions, students need parentheses around each comparison. They should also use the Pandas operators for element by element comparisons, rather than the ordinary Python words used for single true or false values.

Cleaning data requires careful decisions. A blank cell may mean information was never collected, a value is unknown, or a response does not apply. These cases can have different meanings.

Removing every row with a missing value can shrink a data set and may unfairly remove one group more than another. Filling missing values can be useful, but a replacement such as zero changes the meaning of the data. Before cleaning, inspect the number of missing entries in each column and consider why they are missing.

Data types matter too. A column that looks numeric may be stored as text because it contains commas, currency symbols, or words such as unknown.

Grouping is a way to compare categories fairly. Pandas first separates rows into groups, performs a calculation within each group, then combines the results into a smaller table. For example, a school survey can be grouped by grade level before finding an average sleep time.

An overall average may hide important differences between groups. Sorting can make the grouped results easier to read, especially when looking for the highest or lowest values. Students should check the number of rows in every group because an average based on two responses is less reliable than one based on two hundred.

Real data often comes from a CSV file exported from a survey, spreadsheet, sensor, or public website. Column names may contain spaces, inconsistent capital letters, or hidden extra spaces. Checking names early prevents many selection errors.

It is good practice to inspect a few rows, inspect the data types, count missing values, then make a small change and inspect again. Keep the original data unchanged when possible.

Assign cleaned results to a new DataFrame with a clear name. This makes work easier to test, explain, and correct when a later result looks suspicious.