1

My df looks like:

ID     IntakeDate Quantity Converge
6001   3-Jul-52       WB        T
6001  17-May-57       WB        F
6001   3-Jul-52       AD        T
6001  17-May-57       AD        F

I want to read the 'Converge' column for IntakeDate == '3-Jul-52' and Quantity =='WB'. Here's my code:

df_1 = df.loc[(df['IntakeDate']=='3-Jul-52')]
df_2 = df_1.loc[(df_1['Quantity']=='WB')]
convergence = df_2.loc[df_2,'Converge']

Is there a better or easier way to do this?

1 Answer 1

3

You can chain conditions with & for bitwise AND or | for bitwise OR:

df_1 = df.loc[(df['IntakeDate']=='3-Jul-52') & (df['Quantity']=='WB'), 'Converge']

Or use query:

df_1 = df.query("IntakeDate=='3-Jul-52' & Quantity=='WB'")['Converge']

print (df_1)
0    T
Name: Converge, dtype: object
Sign up to request clarification or add additional context in comments.

3 Comments

df.loc[(df['IntakeDate']=='3-Jul-52') & (df['Quantity']=='WB'), 'Converge'].item() or df.loc[(df['IntakeDate']=='3-Jul-52') & (df['Quantity']=='WB'), 'Converge'].iloc[0]
I think there are possible 3 ways of output, so better is use this solution.
and instead a = df.loc[df['Col 1'] == a, 'Col 2'] use a = df.loc[(df['IntakeDate']=='3-Jul-52') & (df['Quantity']=='WB'), 'Converge'] or a = df.query("IntakeDate=='3-Jul-52' & Quantity=='WB'")['Converge']

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.