简体   繁体   中英

Get row numbers based on column values from numpy array

I am new to numpy and need some help in solving my problem. I read records from a binary file using dtypes, then I am selecting 3 columns

df = pd.DataFrame(np.array([(124,90,5),(125,90,5),(126,90,5),(127,90,0),(128,91,5),(129,91,5),(130,91,5),(131,91,0)]), columns =  ['atype','btype','ctype'] )

which gives

   atype    btype   ctype
0    124       90       5
1    125       90       5
2    126       90       5
3    127       90       0
4    128       91       5
5    129       91       5
6    130       91       5
7    131       91       0

'atype' is of no interest to me for now. But what I want is the row numbers when (x,90,5) appears in 2nd and 3rd columns (x,90,0) appears in 2nd and 3rd columns when (x,91,5) appears in 2nd and 3rd columns and (x,91,0) appears in 2nd and 3rd columns etc

There are 7 variables like 90,91,92,93,94,95,96 and correspondingly there will be values of either 5 or 0 in the 3rd column.

The entries are 1 million. So is there anyway to find out these without a for loop.

Using pandas you could try the following.

df[(df['btype'].between(90, 96)) & (df['ctype'].isin([0, 5]))]

Using your example. if some of the values are changed, such that df is

   atype    btype   ctype
0    124       90       5
1    125       90       5
2    126        0       5
3    127       90     100
4    128       91       5
5    129        0       5
6    130       91       5
7    131       91       0

then using the solution above, the following is returned.

   atype    btype   ctype
0    124       90       5
1    125       90       5
4    128       91       5
6    130       91       5
7    131       91       0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM