Home > Mobile >  Comparing column values in a particular row
Comparing column values in a particular row

Time:11-26

I have a pandas data frame with 461 rows and 16 columns.

I need to:

  1. Get the only row with value x (string) in the first column.
  2. Get the column name of the only cell in that particular row that has a value (int) smaller than y.

I know how to get the correct row with the "df.loc" method, but I don't know how to compare the values in that particular row and then get the column name of the cell that matches the condition (smaller than y).

CodePudding user response:

is this what you want?

1.

x = '133'
row_id  = df[df[df.columns[0]] == x].index[0]
print(row_id)

2.

y = 16
row = df.select_dtypes(include=['int64']).loc[row_id]
print(' column name = ', row[row < y].index[0])

enter image description here

  • Related