Akshay
Akshay

Reputation: 2785

Getting the row and column numbers that meets multiple conditions in Pandas

I am trying to get the row and column number, which meets three conditions in Pandas DataFrame.

I have a DataFrame of 0, 1, -1 (bigger than 1850); when I try to get the row and column it takes forever to get the output.

The following is an example I have been trying to use:

import pandas as pd
import numpy as np

a = pd.DataFrame(np.random.randint(2, size=(1845,1850)))

b = pd.DataFrame(np.random.randint(2, size=(5,1850)))
b[b == 1] = -1

c = pd.concat([a,b], ignore_index=True)

column_positive = []
row_positive = []

column_negative = []
row_negative = []

column_zero = []
row_zero = []

for column in range(0, c.shape[0]):
    for row in range(0, c.shape[1]):
        if c.iloc[column, row] == 1:
            column_positive.append(column)
            row_positive.append(row)
        elif c.iloc[column, row] == -1:
            column_negative.append(column)
            row_negative.append(row)
        else:
            column_zero.append(column)
            row_zero.append(row)

I did some web searching and found that np.where() does something like this, but I have no idea how to do it.

Could anyone tell a better alternative?

Upvotes: 0

Views: 245

Answers (1)

Divakar
Divakar

Reputation: 221704

You are right np.where would be one way to do it. Here's an implementation with it -

# Extract  the values from c into an array for ease in further processing
c_arr = c.values

# Use np.where to get row and column indices corresponding to three comparisons
column_zero, row_zero = np.where(c_arr==0)
column_negative, row_negative = np.where(c_arr==-1)
column_positive, row_positive = np.where(c_arr==1)

If you don't mind having rows and columns as a Nx2 shaped array, you could do it in a bit more concise manner, like so -

neg_idx, zero_idx, pos_idx = [np.argwhere(c_arr == item) for item in [-1,0,1]]

Upvotes: 1

Related Questions