Home > Back-end >  Find the rows in which the (aboslute) value is bigger than
Find the rows in which the (aboslute) value is bigger than

Time:11-08

I have a pandas dataframe like:

one two three
1 3 4
2 4 6
1 3 4
10 3 4
2 4 5
0 3 4
-10 3 4

Now observing the first column (labeled 'one') I would like to find the rows where the value is bigger than say 9. (in this case it would be the fourth )

Ideally, I also would like to find the rows where the absolute value of the value is bigger than say 9 (so that would be fourth and seventh)

How can I do this? (So far I only covert the columns into series and even into series of truths and false but my problem is that my dataframe is huge and I cannot visually inspect it. I need to get the row numbers automatically

CodePudding user response:

you can apply abs and compare and filter by loc:

out = df.loc[df['one'].abs() > 9]

output :

>>>
   one  two  three
3   10    3      4
6  -10    3      4

CodePudding user response:

You could use abs() pandas-abs

df = pd.DataFrame({
'a': [1, 4, -6, 3, 7],
'b': [2, 3, 5, 3, 1],
'c': [4, 2, 7, 1, 3]
})

df[df.a.abs() > 5]

returns two rows 2, 4.

CodePudding user response:

row = {} 
for column in df:
    row_temp = {}
    index = df.loc[df[column].abs()>=9].index
    row.update({column:list(index)})
  • Related