-1

Can someone please help me with this. I want to call rows by name, so I used set_index on the 1st column in the dataframe to index the rows by name instead of using integers for indexing.

# Set 'Name' column as index on a Dataframe
df1 = df1.set_index("Name", inplace = True)
df1

Output:

AttributeError: 'NoneType' object has no attribute 'set_index'

Then I run the following code:

result = df1.loc["ABC4"]
result

Output:

AttributeError: 'NoneType' object has no attribute 'loc'

I don't usually run a second code that depends on the 1st before fixing the error, but originally I run them together in one Jupyter notebook cell. Now I see that the two code cells have problems.

Please let me know where I went wrong. Thank you!

The Thonnu
  • 3,578
  • 2
  • 8
  • 30
user6688
  • 31
  • 7

2 Answers2

1

Maybe you should define your dataframe?

import pandas as pd
df1 = pd.DataFrame("here's your dataframe")
df1.set_index("Name")

or just

import pandas as pd
df1 = pd.DataFrame("here's your dataframe").set_index("Name")
df1
vovakirdan
  • 345
  • 3
  • 11
0

Your variable "df1" is not defined anywhere before doing something with it. Try this:

# Set 'Name' column as index on a Dataframe
df1 = ''
df1 = df1.set_index("Name", inplace = True)

If its defined before, its value is NONE. So check this variable first. The rest of the code "SHOULD" work afterwards.

  • Sadly, adding "inplace=True" didn't work at all. So when I deleted the "inplace" part, I got the result I wanted. But thanks for answering so quickly. – user6688 Mar 03 '22 at 12:01