Home > Mobile >  Python Pandas - find row based on two column conditions
Python Pandas - find row based on two column conditions

Time:07-13

I have this table for example:

Example Data

If your_pack_size is the same as competitor_pack_size, then your_measurement should not be the same as competitor_measurement

I would like to find the rows where this condition is broken (if any)

This is what I have tried already:

if (df_pr['your_pack_size'].any() == df_pr['competitor_pack_size'].any()):

  print('your_pack_size is the same as competitor_pack_size')

  print(df_pr.loc[df_pr['your_pack_size'].any() == df_pr['competitor_pack_size'].any()]['competitor_product_id'])

but I get an error. How can I print out the competitor_product_id where the condition is broken?

CodePudding user response:

df[(df['your_pack_size'] == df['competitor_pack_size']) & (df['your_measurement'] == df['competitor_measurement'])]

i.e. get the rows where the sizes are the same AND the measurements are the same

See conditional selection in pandas, very handy to know https://www.includehelp.com/python/conditional-selection-in-the-dataframe-pandas-dataframe.aspx

  • Related