Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question to Loc
#1
Hi there,
this is my table:
State Gender Year Top Name Occurences
0 AK F 1910 Mary 14
1 AK F 1911 Mary 12
2 AK F 1912 Mary 9

(Tha table has a lot of different names, state etc.)

I want to select the column "Top Name", where Name == Michael.
This is my code:
df= babynames.loc[babynames.'Top Name'=='Michael', :]

The problem is, that this column name has an "escape" Top_Name
So I don't know how to write this in my code...

Thank you!
J
Reply
#2
(May-10-2018, 11:15 AM)Jack_Sparrow Wrote: ....
df= babynames.loc[babynames.'Top Name'=='Michael', :]

This should have given you exception. You may use dot reference to a column only if it can be tokenized to a legal Python name - which is not the case. Second selector is redundant
df= babynames.loc[babynames['Top Name']=='Michael']
will do the trick

PS And now mods will give you grief for bad formatting - and they will be right
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Great! Thank you!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020