FAQ: Creating, Loading, and Selecting Data with Pandas - Setting indices

This community-built FAQ covers the “Setting indices” exercise from the lesson “Creating, Loading, and Selecting Data with Pandas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise Setting indices

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

FYI to DEVS: hints for this exercise are missing.

3 Likes

This lesson feels way more confusing than others that I’m used to. I don’t totally understand inplace or drop. Also the cheetsheet is missing that and much more. I guess I’m a little more lost than normal. Maybe this is alright, or I’m only given the basics that I need and more will be obvious later. I’m not sure but it does feel different.

FYI I’m going through the Data Science career path.

1 Like

The reset_index() didn’t work for this last exercise. Because the index of this data frame are still the way they are and not arranged

Same here. No example code or hints. Feels like we’re expected to predict the outcome. External help required to solve this exercise, even with the previous Codecademy content in mind.

If you are having trouble with no.3 this code will produce the correct output

import codecademylib
import pandas as pd

df = pd.DataFrame([
[‘January’, 100, 100, 23, 100],
[‘February’, 51, 45, 145, 45],
[‘March’, 81, 96, 65, 96],
[‘April’, 80, 80, 54, 180],
[‘May’, 51, 54, 54, 154],
[‘June’, 112, 109, 79, 129]],
columns=[‘month’, ‘clinic_east’,
‘clinic_north’, ‘clinic_south’,
‘clinic_west’]
)

df2 = df.loc[[1, 3, 5]]
print(df2)
df3 = df2.reset_index()
df2 = df2.reset_index(drop=True)

Hey!

How does the .loc() method differ from the iloc() method?

greetings!
Marcus

1 Like

I have this code but it doesn’t print the new DataFrame with the new indices. Why?

import codecademylib
import pandas as pd

df = pd.DataFrame([
  ['January', 100, 100, 23, 100],
  ['February', 51, 45, 145, 45],
  ['March', 81, 96, 65, 96],
  ['April', 80, 80, 54, 180],
  ['May', 51, 54, 54, 154],
  ['June', 112, 109, 79, 129]],
  columns=['month', 'clinic_east',
           'clinic_north', 'clinic_south',
           'clinic_west']
)
#print(df)
df2 = df.iloc[[1, 3, 5]]
#print(df2)

df3 = df2.reset_index(inplace=True, drop=True)
print(df3)

The keyword argument of inplace=True is designed to modify the current dataframe instead of returning a new one.

So I should be printing df2 instead of df3?

I’m afraid I don’t know what the instructions require of you but the using a method of df2 with in-place set to True is a way to modify df2. If that’s not what you wanted then drop the in-place argument.

I just want to print the new DataFrame with the resetted indexes.

If you use in-place there is no new dataframe, it would return None. So if you do want a new dataframe, don’t use that option.

I don’t mind if there is a new DataFrame or old DataFrame. I just want to print a DataFrame with the indexes resetted.
In my example df2 is a DataFrame with indexes 1, 3, 5. And df3 should be the same DataFrame with the indexes resetted: 0, 1, 2.
How do I print this last DataFrame?
Mi code doesn’t show that:

df3 = df2.reset_index(inplace=True, drop=True)
print(df3)

In exercise 14, a new method “loc” is introduced without explanation of what it does? and how it differs from “iloc”?

Can anyone help out?

Fellow learner here and I had the same question. This might help

It seems that a variable isn’t needed if you use the ‘inplace’ keyword. You still need it if you’re just using the reset_index() with no arguments, or just the ‘drop’ keyword as an argument.

Just what I observed by testing them out a few times. I could be wrong lol

Me too. With the two arguements, I didn’t find any change