简体   繁体   中英

Filter data frame values using another data frame with boolean values

I am working with multiple data frames. Each dataframe contains numerical data which is of dimension 67 rows x 215 columns. To select the data from each data frame, one more data frame is present with same dimensions and contains boolean values. I am not able to retrieve cell values meeting true condition. Sample code is given below.

    import pandas as pd
    import numpy as np
    
    #initialize a dataframe
    df = pd.DataFrame(
        [[21, 72, 67.1],
        [23, 78, 69.5],
        [32, 74, 56.6],
        [52, 54, 76.2]],
        columns=['a', 'b', 'c'])
print('DataFrame\n----------\n', df)
print('\nDataFrame datatypes :\n', df.dtypes)

#convert pandas dataframe to numpy array

    arr = df.to_numpy()
    
    print('\nNumpy Array\n----------\n', arr)
    print('\nNumpy Array Datatype :', arr.dtype)
    k = np.random.randint(250,275,(4,3))
    print(k)
    kt = pd.DataFrame(k)
    print(kt)
    kb = kt>260
    print(kb)
    km = kb.to_numpy()
    print(km)
    xt = arr(km)
    print(xt)

I sincerely appreciate your time for solving the issue. Thankyou.

You are calling array named arr(as it is numpy.ndarray and it is not a function so you can't call it) instead of passing your boolean mask 'km' in it.so,

instead of using:-

xt = arr(km)

use:-

xt = arr[km]

Now if you print xt you will get:-

array([21. , 23. , 56.6, 52. , 54. ])

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