3

I have a DataFrame say(df) in which columns are state , city , pincode.

I can select all the rows for particular city in a state like this.

requiredState = 'ABC'
requiredCity  = 'XYZ'

newDf = df[ (df['city']== requiredCity) & (df['state']==requiredState) ]

Now I want to select all the rows for all cities of a state.

So this can be done by simply removing the city condition like:

newDf = df[ (df['state'] == requiredState) ]

This will give me all the cities of a state.

My question is, Is there some method in which I need not to remove the city condition and it will result to all the cities in a state?

i.e. something like

requiredCity = ALL
nDf = df[ (df['city']== requiredCity) & (df['state']==requiredState) ]

or Can we pass a list of required cities instead of ALL?

i.e.

requiredCity = ['city1','city2']
2
  • thank you very much for edit. Commented Nov 12, 2018 at 11:04
  • 1
    your welcome Jezrael. Keep sharing knowledge. Commented Nov 12, 2018 at 11:51

2 Answers 2

4

I think you need isin but requiredCity has to be in list, so is possible add new condition chained with | (or) of original condition:

df = pd.DataFrame({'city':list('abcdef'),
                   'pincode':[4,5,4,5,5,4],
                   'state':list('aaabbb')})

print (df)
  city  pincode state
0    a        4     a
1    b        5     a
2    c        4     a
3    d        5     b
4    e        5     b
5    f        4     b


requiredState = 'a'
requiredCity = ['ALL']

m1 = ('ALL' in requiredCity) | (df['city'].isin(requiredCity))
m2 = (df['state']==requiredState)
newDf = df[m1 & m2]
print (newDf)
  city  pincode state
0    a        4     a
1    b        5     a
2    c        4     a

requiredState = 'a'
requiredCity = ['a','c']

m1 = ('ALL' in requiredCity) | (df['city'].isin(requiredCity))
m2 = (df['state']==requiredState)
newDf = df[m1 & m2]
print (newDf)
  city  pincode state
0    a        4     a
2    c        4     a

requiredState = 'a'
requiredCity = ['a']

m1 = ('ALL' in requiredCity) | (df['city'].isin(requiredCity))
m2 = (df['state']==requiredState)
newDf = df[m1 & m2]
print (newDf)
  city  pincode state
0    a        4     a
Sign up to request clarification or add additional context in comments.

Comments

1
 requiredCity = ['city1', 'city2']
 df[(df['city'].apply(lambda x : x in requiredCity)) & (df['state'] == requiredState)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.