Search code examples
pythonpandasmultiple-columnsrowsselection

Pandas: Selecting rows in a table


I am trying to select these rows (where T-stage = 3 AND N-stage = 0 AND Radiation = 1) from three columns (T-stage, N-stage, and Radiation) with Python from the Table below. I used the following but the results is not what was expected:

df=pd.read_csv('Mydata.csv') // Loading my data

#I tried the two approaches below, but the results were not what I expected.

A = ((df['T-stage'] == 3) | (df['N-stage'] == 0 | (df['Radiation'] == 1)))

or

B = ((df['T-stage'] == 3) & (df['N-stage'] == 0 & (df['Radiation'] == 1)))

Mydata


Solution

  • Seems some () mismatch, for each condition use one ():

    B = df[(df['T-stage'] == 3) & (df['N-stage'] == 0) & (df['Radiation'] == 1)]