【问题标题】:How to select the elements of a Pandas DataFrame given a Boolean mask?How to select the elements of a Pandas DataFrame given a Boolean mask?
【发布时间】:2022-12-02 03:11:36
【问题描述】:

I was wondering wether, given a boolean mask, there is a way to retreive all the elements of a DataFrame positioned in correspondance of theTruevalues in the mask.

In my case I have a DataFrame containing the values of a certain dataset, for example let's take the following :

l = [[5, 3, 1], 
     [0, 3, 1], 
     [7, 3, 0], 
     [8, 5, 23], 
     [40, 4, 30],
     [2, 6, 13]]
df_true = pd.DataFrame(l, columns=['1', '2', '3'])
df_true

Then I randomly replace some of the values with 'np.nan' as follows:

l2 = [[5, 3, np.nan], 
     [np.nan, 3, 1], 
     [7, np.nan, 0], 
     [np.nan, 5, 23], 
     [40, 4, np.nan],
     [2, np.nan, 13]]
df_nan= pd.DataFrame(l2, columns=['1', '2', '3'])
df_nan

Let's say that after applying some imputation algorithm I obtained as a result:

l3 = [[5, 3, 1], 
     [2, 3, 1], 
     [7, 8, 0], 
     [8, 5, 23], 
     [40, 4, 25],
     [2, 6, 13]]
df_imp= pd.DataFrame(l3, columns=['1', '2', '3'])
df_imp

Now I would like to create two lists (or arrays), one containing theimputedvalues and the other one thetruevalues in order to compare them. To do so I first created a mask m = df_nan.isnull() which has valueTruein correspondance of the cells containing the imputed values. By applying the mask as df_imp[m] I obtain:

     1       2       3
0   NaN     NaN     1.0
1   2.0     NaN     NaN
2   NaN     8.0     NaN
3   8.0     NaN     NaN
4   NaN     NaN     25.0
5   NaN     6.0     NaN

Is there a way to get instead only the values without also theNan, and put them into a list?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    You can use df.values to return a numpy representation of the DataFrame then use numpy.isnan and keep other values.

    import numpy as np
    arr = df.values
    res = arr[~np.isnan(arr)]
    print(res)
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2023-02-01
      • 2023-02-25
      • 2022-12-01
      • 2022-12-27
      • 2022-12-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-27
      相关资源
      最近更新 更多