0

I have a dictionary as below

city = {"AP":"VIZAG", "TELANGANA":"HYDERABAD"}

and also I have a list which I need to loop for all state tables as below

states = ['AP','HYDERABAD']

for st in states:
 df = spark.sql(f"""select * from {st} where city = {city}["{st}"]""")

In above df I am trying to filter city based on dictionary value as per state. But I am not able to do it

1
  • '{city[st]}' not {city}["{st}"] Commented Mar 23, 2023 at 12:24

1 Answer 1

0

New answer

By combining two filter conditions you can do the expected filtering.

selected_city = 'AP'
df = df.filter(
    (F.col('city') == selected_city)
    & (F.col('state') == cities[selected_city])
)

Old answer

It is a simple change: You can use isin to filter a column based on a list [Docs].

cities = list(city.keys())

df = df.filter(F.col('city').isin(cities))

If you want to construct more complex conditions based on a dictionary see this question.

[Edit] Updated answer based on OPs comment. Will leave the old one in there for completeness.

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

3 Comments

Thanks for your response. But I need to filter only if state='AP' then city should filter with'VIZAG' which is the dictionary value for AP
Cool. Just edited the answer, hope I understood what you mean.
Thanks for your help

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.