Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge 3: Indexing and MultiIndexing | Pandas
Data Science Interview Challenge

book
Challenge 3: Indexing and MultiIndexing

Pandas, an indispensable library in the data scientist's toolkit, offers robust indexing capabilities which are integral for data manipulation and retrieval.

  • Efficiency: Fast data access and manipulation is often dependent on smart indexing strategies, especially for larger datasets.

  • Flexibility: Whether it's basic row/column labels, hierarchical labels, or even date-time based indexing, Pandas has got you covered.

  • Readability: Descriptive indexing can render the code more intuitive and easier to follow, thereby streamlining the data exploration phase.

A solid grasp of indexing techniques, inclusive of multi indexing, can expedite tasks such as data retrieval, aggregation, and restructuring.

Oppgave

Swipe to start coding

Dive into indexing with Pandas through these tasks:

  1. Set a column Date as the index of a DataFrame.
  2. Reset the index of a DataFrame.
  3. Create a DataFrame with a MultiIndex.
  4. Access data from a MultiIndexed DataFrame with indices A and 1.

Løsning

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-02', '2023-01-03'],
'Temperature': [22, 24, 23],
'Humidity': [56, 58, 57]
})

# 1. Set a column as the index of a DataFrame.
indexed_df = df.set_index('Date')
display(indexed_df)
print('-' * 40)

# 2. Reset the index of a DataFrame.
reset_df = indexed_df.reset_index()
display(reset_df)
print('-' * 40)

# 3. Create a DataFrame with a MultiIndex.
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
multi_indexed_df = pd.DataFrame({
'Value': [10, 20, 30, 40]
}, index=pd.MultiIndex.from_arrays(arrays, names=('Letter', 'Number')))
display(multi_indexed_df)
print('-' * 20)

# 4. Access data from a MultiIndexed DataFrame.
retrieved_data = multi_indexed_df.loc['A', 1]
display(retrieved_data)

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
single

single

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-02', '2023-01-03'],
'Temperature': [22, 24, 23],
'Humidity': [56, 58, 57]
})

# 1. Set a column as the index of a DataFrame.
indexed_df = ___
display(indexed_df)
print('-' * 40)

# 2. Reset the index of a DataFrame.
reset_df = ___
display(reset_df)
print('-' * 40)

# 3. Create a DataFrame with a MultiIndex.
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
multi_indexed_df = pd.DataFrame({
'Value': [10, 20, 30, 40]
}, index=pd.___.___(arrays, ___=('Letter', 'Number')))
display(multi_indexed_df)
print('-' * 20)

# 4. Access data from a MultiIndexed DataFrame.
retrieved_data = multi_indexed_df.___
display(retrieved_data)

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt