3

I am trying to use multiple conditions to select multiple columns on a pandas DataFrame using the following:

df = pd.DataFrame(wks.get_all_records())
print(df[df["COLUMN_NAME"] == 1][df["COLUMN_NAME_2"] == "CONDITION_2"][["COL_A", "COL_B", "COL_C"]])

It does work, but also returns:

UserWarning: Boolean Series key will be reindexed to match DataFrame index

I'm trying to filter the DataFrame using conditions and write it to an Excel worksheet after it, so I'm not sure if this warning will have some impact afterwards.

cottontail
  • 10,268
  • 18
  • 50
  • 51
xDMed
  • 31
  • 1
  • 2
  • 3
    The recommended way would be to use ``.loc``, ``print(df.loc[((df["COLUMN_NAME"] == 1) & (df["COLUMN_NAME_2"] == "CONDITION_2")), ["COL_A", "COL_B", "COL_C"]])`` – sushanth Jun 10 '20 at 13:45
  • Does this answer your question? [Boolean Series key will be reindexed to match DataFrame index](https://stackoverflow.com/questions/41710789/boolean-series-key-will-be-reindexed-to-match-dataframe-index) – cottontail Oct 20 '22 at 17:46

1 Answers1

0

This is an expansion of @sushanth answer n the 1st comment on the question. There are other answers around but non that I found had this same problem. I wanted a subset of columns that fulfil Boolean conditions and could not find how. The warning means that the behavior might change later and now it will do it implicitly, which is not good for code maintainability.

df.loc[(boolean series), list_col_header]

In that order, then

df.loc[((df["COLUMN_NAME"] == 1) & 
         (df["COLUMN_NAME_2"] == "CONDITION_2")), ["COL_A", "COL_B", "COL_C"]]

Do not forget to put the Boolean series in parenthesis and each Boolean condition in parenthesis too. Use logical operators such as & | instead of and or

CaribeGirl
  • 163
  • 2
  • 12