Skip to content
Advertisement

Slice pandas row of a specific column using numpy boolean

The objective is to slice multiple pandas row of a specific column using a Numpy boolean.

The following code should do the task

import numpy as np
import numpy.random
import pandas as pd
numpy.random.seed(0)

h=((), ('x'), (), ('y'), (), (), ())

drop_idx=[n for n, dl in enumerate(h) if len(dl)]

df = pd.DataFrame(np.arange(7),columns=['class'])

df.reset_index(inplace=True)
df2=pd.DataFrame(np.arange(5),columns=[('feature','ch1')])

idx_true=np.invert(np.array(h).astype(bool))
g=df[idx_true.tolist()].reset_index(drop=True)
df2['dlabel']=g['class']

However, I wonder whether the above code can be shortened further, especially these lines

idx_true=np.invert(np.array(h).astype(bool))
g=df[idx_true.tolist()].reset_index(drop=True)
df2['dlabel']=g['class']

Currently, Pandas throw an error if I am to directly using Numpy boolean without converting to list

df[idx_true.tolist()]

Is there something I miss, or this is the only way to achieve the intended objective?

Advertisement

Answer

You can simply use:

df2['dlabel'] = df.loc[idx_true, 'class'].values

You actually don’t even need to convert h to a numpy array:

df2['dlabel'] = df.loc[[not bool(x) for x in h], 'class'].values

output:

   (feature, ch1)  dlabel
0  0               0     
1  1               2     
2  2               4     
3  3               5     
4  4               6     
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement