0

I'd like to look for info 'months_in_service" that could be 1, 2 or 3. Instead of put the code three times, what I can do instead?

axx = clean_df.loc[(clean_df['service_part_number'] == pn_list[3]) & (clean_df['months_in_service'] == 1), 'service_technician_comment']
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    `clean_df['months_in_service'].isin([1,2,3])` – perl Mar 09 '21 at 16:33
  • 1
    Check out this post for more info: [How to select rows from a DataFrame based on column values](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – perl Mar 09 '21 at 16:35

2 Answers2

1

You can use .isin([<list of items>])

axx = clean_df.loc[(clean_df['service_part_number'] == pn_list[3]) & (clean_df['months_in_service'].isin([1, 2, 3]), 'service_technician_comment']
Niels Henkens
  • 2,553
  • 1
  • 12
  • 27
0

You may want to give the isin method a shot.

axx = clean_df.loc[(clean_df['service_part_number'] == pn_list[3]) & (clean_df['months_in_service'].isin([1,2,3])), 'service_technician_comment']

If you use the dictionary implementation of isin, you should be able to get the same results while placing whatever equal predicates you may have:

axx = clean_df[clean_df.isin({"service_part_number": [pn_list[3]], "months_in_service", [1,2,3]})]["service_technician_comment"]

Just keep in mind, you cannot use loc when using isin like this, as it will raise: ValueError: Cannot index with multidimensional key.

Dharman
  • 30,962
  • 25
  • 85
  • 135
trozzel
  • 479
  • 5
  • 12