4

I want to replicate what where clause does in SQL, using Python. Many times conditions in where clause can be complex and have multiple conditions. I am able to do it in the following way. But I think there should be a smarter way to achieve this. I have following data and code.

My requirement is: I want to select all columns only when first letter in the address is 'N'. This is the initial data frame.

enter image description here

d = {'name': ['john', 'tom', 'bob', 'rock', 'dick'], 'Age': [23, 32, 45, 42, 28], 'YrsOfEducation': [10, 15, 8, 12, 10], 'Address': ['NY', 'NJ', 'PA', 'NY', 'CA']}
import pandas as pd
df = pd.DataFrame(data = d)
df['col1'] = df['Address'].str[0:1] #creating a new column which will have only the first letter from address column
n = df['col1'] == 'N' #creating a filtering criteria where the letter will be equal to N
newdata = df[n] # filtering the dataframe 
newdata1 = newdata.drop('col1', axis = 1) # finally dropping the extra column 'col1'

So after 7 lines of code I am getting this output:

enter image description here

My question is how can I do it more efficiently or is there any smarter way to do that ?

1 Answer 1

4

A new column is not necessary:

newdata = df[df['Address'].str[0] == 'N'] # filtering the dataframe 
print (newdata)
  Address  Age  YrsOfEducation  name
0      NY   23              10  john
1      NJ   32              15   tom
3      NY   42              12  rock
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to both of you, it is a better way indeed. However, if I have multiple conditions in the where clause will this work ? Like I tried following but getting error: TypeError: cannot compare a dtyped [int64] array with a scalar of type [bool] Code: newdata4 = df[df['Address'].str[0] == 'N' & df['Age'] > 30]
@singularity2047 - You are rely close, need () only like newdata4 = df[(df['Address'].str[0] == 'N') & (df['Age'] > 30)]

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.