0

I am trying to get the subset dataframe using the below code:

dprev = "eiffel tower"

df.loc[df['place'] == dprev] -> returns empty

drandom = random.choice(df['place'].unique())

df.loc[df['place'] == drandom] -> returns the subset

why am i not seeing the same thing when dprev is string variable?

Sumit S Chawla
  • 3,180
  • 1
  • 14
  • 33
user3779226
  • 31
  • 1
  • 4

3 Answers3

1

Can you try using str.contains with case=False

Ex:

import pandas as pd

dprev = "eiffel tower"
df = pd.DataFrame({"place": ["eiffel tower", "Eiffel tower", "Hello"], "data":[1,2,3]})
print(df.loc[df['place'].str.contains(dprev, case=False)])

Output:

   data         place
0     1  eiffel tower
1     2  Eiffel tower
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Hi Rakesh, tried with the case = False, but ended with empty dataframe. To help further this time - dprev = Avenue Victoria, Paris, France Is the comma is creating issue? – user3779226 Jun 01 '18 at 10:47
  • Can you post a sample of you csv in your question... I am not able to understand the data in your comment. – Rakesh Jun 01 '18 at 10:48
  • Can you post your DF also? – Rakesh Jun 01 '18 at 11:04
0

for this you don't have to use df.loc

dprev = "eiffel tower"

df[df['place'] == dprev] 

See this answer

Nihal
  • 5,262
  • 7
  • 23
  • 41
0

Try in this way:

dprev = "eiffel tower"
df = df.loc[df['place'].str.lower() == dprev]

or:

df = df.loc[df['place'].str.lower().str.contains(dprev)]
Joe
  • 12,057
  • 5
  • 39
  • 55