1

Given this pandas.core.frame.DataFrame sample:

        col
0   2   0
    0   1
    3   1

I would like to get those indexes where col is 1:

df[df['col']==1]

The expected result would be a vector (0,3).

jpp
  • 159,742
  • 34
  • 281
  • 339
PeCaDe
  • 277
  • 1
  • 8
  • 33

2 Answers2

3

You can use NumPy to create an array of indices, and then filter for the second index:

import numpy as np

df = pd.DataFrame({'col': [0, 1, 1]},
                  index=pd.MultiIndex.from_tuples([(0, 2), (0, 0), (0, 3)]))

#      col
# 0 2    0
#   0    1
#   3    1

idx = np.array(df[df['col'] == 1].index.tolist())

# array([[0, 0],
#        [0, 3]])

res = idx[:, 1]

# array([0, 3])
jpp
  • 159,742
  • 34
  • 281
  • 339
1

This might do the trick:

import pandas as pd

d = {'base':[2,0,3],'col':[0,1,1]}
df = pd.DataFrame(data=d)
col_is_1 = df['col'] == 1
df_temp = df[col_is_1]
matrix = df_temp.values
vec = matrix[:,0]
print(vec)

return [0,3] as expected

Sharku
  • 1,052
  • 1
  • 11
  • 24