Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge 4: Altering DataFrame | Pandas
Data Science Interview Challenge

book
Challenge 4: Altering DataFrame

Pandas provides a plethora of tools that allow for easy modification of both data and structure of DataFrames. These capabilities are essential because:

  • Data Cleaning: Real-world datasets are often messy. The ability to transform and clean data ensures its readiness for analysis.

  • Versatility: Frequently, the structure of a dataset may not align with the requirements of a given task. Being able to reshape data can be a lifesaver.

  • Efficiency: Direct modifications to DataFrames, as opposed to creating new ones, can save memory and improve performance.

Getting familiar with the techniques to alter data and the structure of DataFrames is a key step in becoming proficient with Pandas.

Uppgift

Swipe to start coding

Harness the power of Pandas to alter data and the structure of DataFrames:

  1. Add a new column to a DataFrame with values Engineer, Doctor and Artist.
  2. Rename columns in a DataFrame. Change the Name column into Full Name and the Age column into Age (years).
  3. Drop a column City from a DataFrame.
  4. Sort a DataFrame based on the Age column (descending).

Lösning

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'San Francisco', 'Los Angeles']
})

# 1. Add a new column to a DataFrame.
df['Occupation'] = ['Engineer', 'Doctor', 'Artist']
display(df)
print('-' * 40)

# 2. Rename columns in a DataFrame.
renamed_df = df.rename(columns={'Name': 'Full Name', 'Age': 'Age (years)'})
display(renamed_df)
print('-' * 40)

# 3. Drop a column from a DataFrame.
reduced_df = renamed_df.drop('City', axis=1)
display(reduced_df)
print('-' * 40)

# 4. Sort a DataFrame based on a specific column.
sorted_df = reduced_df.sort_values(by='Age (years)', ascending=False)
display(sorted_df)

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'San Francisco', 'Los Angeles']
})

# 1. Add a new column to a DataFrame.
df['Occupation'] = ___
display(df)
print('-' * 40)

# 2. Rename columns in a DataFrame.
renamed_df = ___
display(renamed_df)
print('-' * 40)

# 3. Drop a column from a DataFrame.
reduced_df = ___
display(reduced_df)
print('-' * 40)

# 4. Sort a DataFrame based on a specific column.
sorted_df = ___
display(sorted_df)

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt