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.