Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Modifying Data Structure | Pandas Foundations
Introduction to Pandas with AI

bookModifying Data Structure

AI in Action

12345678910
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) students["Passed"] = students["Grade"] > 70 print(students)
copy

Adding and Removing Columns

Adding a new column is as simple as assigning values to a new column name. To remove a column, use the .drop() method.

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Add a column students["GradePlus5"] = students["Grade"] + 5 # Remove a column students = students.drop("Grade", axis=1) print(students)
copy

The axis=1 specifies that you want to remove a column.

Adding and Removing Rows

There are two ways to add a new row:

  1. Use .loc[] with a new index label and values;
  2. Use pd.concat() to join two DataFrames.

To remove a row, use the .drop() method.

1234567891011121314151617
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Add a row with loc students.loc[3] = ["Eve", 28, 88] # Add a row with concat new_row = pd.DataFrame({"Name": ["Frank"], "Age": [25], "Grade": [95]}) students = pd.concat([students, new_row], ignore_index=True) # Remove a row students = students.drop(1) print(students)
copy

With ignore_index=True, pd.concat() discards the original index values and generates a new continuous index (0, 1, 2, ...).

Working with Indexes

Whenever you add or remove rows, you might notice that the row labels (the index) no longer form a clean sequence. To fix this, you can reset the index to restore simple row numbering or set one of the columns as the new index if you want labels that are more meaningful:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Set "Name" as the index students = students.set_index("Name") print(students) # Reset to default numeric index students = students.reset_index() print(students)
copy

If you want to leave a simple numeric index, then it is a good idea to call .reset_index(drop=True) each time you add/remove rows to keep your DataFrame tidy.

1. Which line correctly adds a new column named "Total" that is the product of the "Price" and "Quantity" columns?

2. Which method removes a row with label 2 from a DataFrame?

3. Why might you use reset_index(drop=True) after deleting rows?

question mark

Which line correctly adds a new column named "Total" that is the product of the "Price" and "Quantity" columns?

Select the correct answer

question mark

Which method removes a row with label 2 from a DataFrame?

Select the correct answer

question mark

Why might you use reset_index(drop=True) after deleting rows?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 10

bookModifying Data Structure

Swipe to show menu

AI in Action

12345678910
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) students["Passed"] = students["Grade"] > 70 print(students)
copy

Adding and Removing Columns

Adding a new column is as simple as assigning values to a new column name. To remove a column, use the .drop() method.

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Add a column students["GradePlus5"] = students["Grade"] + 5 # Remove a column students = students.drop("Grade", axis=1) print(students)
copy

The axis=1 specifies that you want to remove a column.

Adding and Removing Rows

There are two ways to add a new row:

  1. Use .loc[] with a new index label and values;
  2. Use pd.concat() to join two DataFrames.

To remove a row, use the .drop() method.

1234567891011121314151617
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Add a row with loc students.loc[3] = ["Eve", 28, 88] # Add a row with concat new_row = pd.DataFrame({"Name": ["Frank"], "Age": [25], "Grade": [95]}) students = pd.concat([students, new_row], ignore_index=True) # Remove a row students = students.drop(1) print(students)
copy

With ignore_index=True, pd.concat() discards the original index values and generates a new continuous index (0, 1, 2, ...).

Working with Indexes

Whenever you add or remove rows, you might notice that the row labels (the index) no longer form a clean sequence. To fix this, you can reset the index to restore simple row numbering or set one of the columns as the new index if you want labels that are more meaningful:

1234567891011121314
import pandas as pd students = pd.DataFrame({ "Name": ["Alice", "Bob", "Carol", "Dan"], "Age": [24, 30, 22, 21], "Grade":[85, 62, 90, 78] }) # Set "Name" as the index students = students.set_index("Name") print(students) # Reset to default numeric index students = students.reset_index() print(students)
copy

If you want to leave a simple numeric index, then it is a good idea to call .reset_index(drop=True) each time you add/remove rows to keep your DataFrame tidy.

1. Which line correctly adds a new column named "Total" that is the product of the "Price" and "Quantity" columns?

2. Which method removes a row with label 2 from a DataFrame?

3. Why might you use reset_index(drop=True) after deleting rows?

question mark

Which line correctly adds a new column named "Total" that is the product of the "Price" and "Quantity" columns?

Select the correct answer

question mark

Which method removes a row with label 2 from a DataFrame?

Select the correct answer

question mark

Why might you use reset_index(drop=True) after deleting rows?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
some-alt