1

Let's say I have a table named park like below.

Lon Lat City
 1   1   A
 1   2   A
 2   3   B
 3   4   C

I want to grab the rows of City A only (the data frame with matching value of "A") so that it looks like below

Lon Lat City
 1   1   A
 1   2   A

How can I do this? Edit: I realized that the example in the first link works but on my data it does not work. Why would this happen?

(My actual data contains Japanese characters so I am used a simplified version above.)

What I tried and did NOT work:

isin

I followed this and this ran below but they both return empty

value_list = ["北海道"] 
park[park.Prefecture.isin(value_list)] # returns empty 
park.loc[park["Prefecture"].isin(["北海道"])] # returns empty

Screenshots

Data enter image description here

Empty result enter image description here

Leonard
  • 2,978
  • 6
  • 21
  • 42
  • `df[df.City.eq('A')]` – anky Jul 21 '19 at 15:21
  • @anky_91 it doesn't work – Leonard Jul 21 '19 at 15:22
  • doesnt work for the example you shared or your actual data? – anky Jul 21 '19 at 15:23
  • @anky_91 actual data.. do you think there is a chance that somehow my titles are not mapped with the csv data? because I did `park.columns = ["Longitude", "Latitude", "Name", "Kind", "Prefecture", "City", "Open Date", "Area", "Planned"]` after I import CSV – Leonard Jul 21 '19 at 15:24
  • 1
    print `df.City.unique()` and see if the value you are searching for is there. There are chances that there are spaces in the name – anky Jul 21 '19 at 15:26
  • `[' 北海道' ' 青森県' ' 岩手県' ... ' 宮崎県' ' 鹿児島県']` Oh my god there are... let me remove them and re-do it – Leonard Jul 21 '19 at 15:28
  • 1
    @anky_91 Got it now. You are a life-saver. Please add it to the answer and I will accept it. – Leonard Jul 21 '19 at 15:30

2 Answers2

1

Your City column has spaces, please remove them :

Either trim the column using =TRIM() before reading into Pandas,

Else df.City=df.City.str.strip() will strip the trailing whitespaces.

Post that use:

final=df[df.City.eq('A')]
anky
  • 74,114
  • 11
  • 41
  • 70
0

You could try using something like this.

df[df['city']=='A']

this will return a dataframe only consisting of City A so you can use that dataframe to get your data.