Skip to content
Advertisement

How to check a value in which column in dataframe

I have a address table like this

JavaScript

I need to extract the ID when I got a name. However, I don’t know the name is a City name or Area name. So I want to use conditional check the name in City or Area and then extract the ID I have tried this method, but it was not run for me.

By the way the name is a string. Ex. name = “AA”

JavaScript

Any other good solution?

Advertisement

Answer

Try this way:

.values will give you an array therefore your if statement can be true, but simply the id would return a series object – I guess you only want the number 1 or 2… therefore if you take the .max(), that will give you the number also if you pass a name that is not in there it will be an empty object and should not fail (unlike you did it with iloc)

JavaScript
Advertisement