More efficient way to use pandas get_loc?

Task: Search for a multi-frame dataframe for a value (all values ​​are unique) and return the index of this row.

Currently: using get_loc, but this only allows you to skip one column at a time, which leads to a rather inefficient set of try except statement. Although this works, does anyone know of a more efficient way to do this?

df =  pd.DataFrame(np.random.randint(0,100,size=(4, 4)), columns=list('ABCD'))
try: 
     unique_index = pd.Index(df['A'])
     print(unique_index.get_loc(20))
except KeyError:
    try: 
        unique_index = pd.Index(df['B'])
        print(unique_index.get_loc(20))
    except KeyError:
                unique_index = pd.Index(df['C'])
                print(unique_index.get_loc(20))

Loops do not seem to work due to KeyError that occur if the column does not contain a value. I looked at features like .contains or .isin, but this is the location index that interests me.

+4
source share
5 answers

np.where, , . .

df =  pd.DataFrame(np.random.randint(0,100,size=(4, 4)), columns=list('ABCD'))
indices = np.where(df.values == 20)
rows = indices[0]
if len(rows) != 0:
    print(rows[0])
+3

, np.random.seed

np.random.seed([3, 1415])
df = pd.DataFrame(
    np.random.randint(200 ,size=(4, 4)),
    columns=list('ABCD'))

df

     A    B    C    D
0   11   98  123   90
1  143  126   55  141
2  139  141  154  115
3   63  104  128  120

, - , , np.where . , 55, , , , . 20, . , , .

i, j = np.where(df.values == 55)
list(zip(df.index[i], df.columns[j]))

[(1, 'C')]
+3

:

df[(df==20).any(axis=1)].index
+3

np.where(), , any().

df.loc[df.isin([20]).any(axis=1)].index

df.loc[*condition_here*] TRUE, , any , true

df:

    A   B   C   D
0   82  7   48  90
1   68  18  90  14 #< ---- notice the 18 here
2   18  34  72  24 #< ---- notice the 18 here
3   69  73  40  86

df.isin([18])

    A   B   C   D
0   False   False   False   False
1   False   True    False   False  #<- ---- notice the TRUE value
2   True    False   False   False  #<- ---- notice the TRUE value
3   False   False   False   False


print(df.loc[df.isin([18]).any(axis=1)].index.tolist())
#output is a list
[1, 2]
+3

df[df.eq(20)].stack()
Out[1220]: 
1  C    20.0
dtype: float64
+3
source

Source: https://habr.com/ru/post/1693934/


All Articles