DPdl
DPdl

Reputation: 755

Reading a column value in Pandas Dataframe

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?

Upvotes: 1

Views: 62

Answers (1)

jezrael
jezrael

Reputation: 862406

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

Upvotes: 3

Related Questions