1

I have a dataframe of information. One column is a rank. I just want to find the row with a rank of 2 and get the another column item (called 'Name'.) I can find the row and get the name but its not a pure text item that I can add to other text. Its an object.

How do I just get the name as text?

Code:

print "The name of the 2nd best is: " + groupDF.loc[(DF['Rank']==2),'Name']

This gives me the id of the row and the Name. I just want the Name

This is what I get:

4    The name of the 2nd best is: Hawthorne

    Name: CleanName, dtype: object

I just can't figure out what to search on to get the answer. I get lots of other stuff but not this answer.

Thanks in advance.

1
  • try: groupDF.iloc[(DF['Rank']==2)]]['Name'] Commented Jan 28, 2014 at 3:29

2 Answers 2

5

In a little bit more detail:

I understand you have a data frame of the kind:

names = ["Almond","Hawthorn","Peach"]
groupDF = pd.DataFrame({'Rank':[1,2,3],'Name':names})

groupDF.loc[(groupDF['Rank']==2),'Name'] gives you a Series object. If the rank is unique then either of the following two possibilities works

groupDF.loc[(groupDF['Rank']==2),'Name'].item()

or

groupDF.loc[(groupDF['Rank']==2),'Name'].iloc[0]

result:

'Hawthorn'

If the rank is not unique, the second one still works and gives you the first hit, that is, the first element of the Series object created by the command.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to call the item() method of the resulting Series object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.