Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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     
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading