Python Forum
Question to Loc - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question to Loc (/thread-10043.html)



Question to Loc - Jack_Sparrow - May-10-2018

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


RE: Question to Loc - volcano63 - May-10-2018

(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


RE: Question to Loc - Jack_Sparrow - May-10-2018

Great! Thank you!