Row selection combining condition on index and condition on a column in a pandas dataframe

Question:

I want to select rows from a dataframe based on values in the index combined with values in a specific column:

df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [0, 20, 30], [40, 20, 30]], 
                  index=[4, 5, 6, 7], columns=['A', 'B', 'C'])


    A   B   C
4   0   2   3
5   0   4   1
6   0  20  30
7  40  20  30

With

df.loc[df['A'] == 0, 'C'] = 99

I can select all rows with column A = 0 and replace the value in column C with 99, but how can I select all rows with column A = 0 and the index < 6. In other words, I want to combine selection on the index with selection on the column.

Asked By: Egirus Ornila

||

Answers:

You can use multiple conditions in your loc statement:

df.loc[(df.index < 6) & (df.A == 0), 'C'] = 99
Answered By: sacuL

The canonical method is to reduce all boolean conditions into a single boolean condition and filter the frame by it.

So for the task at hand, to filter a dataframe by a condition on its index and its columns, write two boolean conditions and reduce into one using & (as suggested by @sacuL).

Some alternative methods:

  • eval() may be used for a readable condition
    df.loc[df.eval('index < 6 and A == 0'), 'C'] = 99
    
  • A function may be passed to loc:
    df.loc[lambda x: (x.index < 6) & (x['A']==0), 'C'] = 99
    
Answered By: cottontail