Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge 5: Iterating Over Data | Pandas
Data Science Interview Challenge

book
Challenge 5: Iterating Over Data

Iterating over datasets in Pandas is a critical operation, especially when custom data processing steps need to be applied to each row or column. Pandas offers:

  • Flexibility: Whether you need to process data row-wise, column-wise, or cell-wise, Pandas has you covered with multiple methods.

  • Efficiency: While it's typically more efficient to use vectorized Pandas operations, sometimes iteration is the most straightforward approach.

Understanding how to iterate effectively over datasets in Pandas can greatly aid in the data cleaning and pre-processing phase.

Compito

Swipe to start coding

Discover different ways to iterate over datasets in Pandas:

  1. Iterate over rows of a DataFrame.
  2. Iterate over column names of a DataFrame.
  3. Apply a custom function to each cell in a DataFrame column.
  4. Use the map function to format the entire DataFrame.

Soluzione

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. Iterate over rows of a DataFrame.
print('--- Task 1 ---')
for index, row in df.iterrows():
print(row)
print('-' * 20)

# 2. Iterate over columns of a DataFrame.
print('\n--- Task 2 ---')
for column in df:
print(column)

# 3. Apply a custom function to each cell in a DataFrame column.
print('\n--- Task 3 ---')
def add_suffix(cell):
return str(cell) + "_suffix"

df['Name'] = df['Name'].apply(add_suffix)
print(df)

# 4. Replace values in the City column according replacement_dict.
print('\n--- Task 4 ---')
replacement_dict = {
'New York': 'NY',
'San Francisco': 'SF',
'Los Angeles': 'LA'
}

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
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. Iterate over rows of a DataFrame.
print('--- Task 1 ---')
for index, row in ___:
print(row)
print('-' * 20)

# 2. Iterate over columns of a DataFrame.
print('\n--- Task 2 ---')
for column in ___:
print(column)

# 3. Apply a custom function to each cell in a DataFrame column.
print('\n--- Task 3 ---')
def add_suffix(cell):
return str(cell) + "_suffix"

df['Name'] = ___
print(df)

# 4. Replace values in the City column according replacement_dict.
print('\n--- Task 4 ---')
replacement_dict = {
'New York': 'NY',
'San Francisco': 'SF',
'Los Angeles': 'LA'
}

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt