【问题标题】:How to select subsequent numpy arrays handling potential np.nan values如何选择处理潜在 np.nan 值的后续 numpy 数组
【发布时间】:2020-04-09 13:54:34
【问题描述】:

我有一个这样的系列:

s = pd.Series({10: np.array([[0.72260683, 0.27739317, 0.        ],
                         [0.7187053 , 0.2812947 , 0.        ],
                         [0.71435467, 0.28564533, 1.        ],
                         [0.3268072 , 0.6731928 , 0.        ],
                         [0.31941951, 0.68058049, 1.        ],
                         [0.31260015, 0.68739985, 0.        ]]), 
           20: np.array([[0.7022099 , 0.2977901 , 0.        ],
                         [0.6983866 , 0.3016134 , 0.        ],
                         [0.69411673, 0.30588327, 1.        ],
                         [0.33857735, 0.66142265, 0.        ],
                         [0.33244109, 0.66755891, 1.        ],
                         [0.32675582, 0.67324418, 0.        ]]), 
           20: np.array([[0.68811957, 0.34188043, 0.        ],
                         [0.68425783, 0.31574217, 0.        ],
                         [0.67994496, 0.32005504, 1.        ],
                         [0.34872593, 0.66127407, 1.        ],
                         [0.34276171, 0.65723829, 1.        ],
                         [0.33722803, 0.66277197, 0.        ]]),
           38: np.array([[0.68811957, 0.31188043, 0.        ],
                         [0.68425783, 0.31574217, 0.        ],
                         [0.67994496, 0.32005504, 1.        ],
                         [0.34872593, 0.65127407, 0.        ],
                         [0.34276171, 0.65723829, 1.        ],
                         [0.33722803, 0.66277197, 0.        ]]),
           np.nan: np.nan}
)

我想用np.array([1, 4, 1, 5])np.array([1, 4, 1, np.nan]) 对其进行子集化,返回np.nan,无论索引数组的最后一个元素的值是什么。我怎样才能做到这一点?

请注意,我不能简单地删除系列的最后一个元素。

【问题讨论】:

    标签: python arrays pandas numpy


    【解决方案1】:

    您可以修改以前的answer,删除Series 的缺失值,最后通过Series.reindex 添加它们(仅Series 的必要唯一索引):

    #a = np.array([1, 4, 1, 5])
    a = np.array([1, 4, 1, np.nan])
    
    mask = s.notna()
    b = np.array(s[mask].tolist())[np.arange(mask.sum()), a[mask].astype(int), 2]
    print (b)
    [0. 1. 0.]
    
    c = pd.Series(b, index=s[mask].index).reindex(s.index)
    print (c)
    10.0    0.0
    20.0    1.0
    38.0    0.0
    NaN     NaN
    dtype: float64
    

    编辑:如果索引中不是唯一值,则需要使用 GroupBy.cumcount 创建唯一的 MultiIndex:

    s = pd.Series({10: np.array([[0.72260683, 0.27739317, 0.        ],
                             [0.7187053 , 0.2812947 , 0.        ],
                             [0.71435467, 0.28564533, 1.        ],
                             [0.3268072 , 0.6731928 , 0.        ],
                             [0.31941951, 0.68058049, 1.        ],
                             [0.31260015, 0.68739985, 0.        ]]), 
               20: np.array([[0.7022099 , 0.2977901 , 0.        ],
                             [0.6983866 , 0.3016134 , 0.        ],
                             [0.69411673, 0.30588327, 1.        ],
                             [0.33857735, 0.66142265, 0.        ],
                             [0.33244109, 0.66755891, 1.        ],
                             [0.32675582, 0.67324418, 0.        ]]), 
               23: np.array([[0.68811957, 0.34188043, 0.        ],
                             [0.68425783, 0.31574217, 0.        ],
                             [0.67994496, 0.32005504, 1.        ],
                             [0.34872593, 0.66127407, 1.        ],
                             [0.34276171, 0.65723829, 1.        ],
                             [0.33722803, 0.66277197, 0.        ]]),
               38: np.array([[0.68811957, 0.31188043, 0.        ],
                             [0.68425783, 0.31574217, 0.        ],
                             [0.67994496, 0.32005504, 1.        ],
                             [0.34872593, 0.65127407, 0.        ],
                             [0.34276171, 0.65723829, 1.        ],
                             [0.33722803, 0.66277197, 0.        ]]),
               np.nan: np.nan}
    ).rename({23:20})
    
    print (s)
    10.0    [[0.72260683, 0.27739317, 0.0], [0.7187053, 0....
    20.0    [[0.7022099, 0.2977901, 0.0], [0.6983866, 0.30...
    20.0    [[0.68811957, 0.34188043, 0.0], [0.68425783, 0...
    38.0    [[0.68811957, 0.31188043, 0.0], [0.68425783, 0...
    NaN                                                   NaN
    dtype: object
    

    a = np.array([1, 4, 1, 2, np.nan])
    
    s = s.to_frame('a').set_index(s.groupby(s.index).cumcount(), append=True)['a']
    print (s)
    10.0  0    [[0.72260683, 0.27739317, 0.0], [0.7187053, 0....
    20.0  0    [[0.7022099, 0.2977901, 0.0], [0.6983866, 0.30...
          1    [[0.68811957, 0.34188043, 0.0], [0.68425783, 0...
    38.0  0    [[0.68811957, 0.31188043, 0.0], [0.68425783, 0...
    NaN   0                                                  NaN
    Name: a, dtype: object
    

    mask = s.notna()
    b = np.array(s[mask].tolist())[np.arange(mask.sum()), a[mask].astype(int), 2]
    print (b)
    [0. 1. 0. 1.]
    
    c = pd.Series(b, index=s[mask].index).reindex(s.index)
    print (c)
    10.0  0    0.0
    20.0  0    1.0
          1    0.0
    38.0  0    1.0
    NaN   0    NaN
    dtype: float64
    

    并在最后一步删除MultiIndex 的助手级别:

    c = c.reset_index(level=-1, drop=True)
    print (c)
    10.0    0.0
    20.0    1.0
    20.0    0.0
    38.0    1.0
    NaN     NaN
    dtype: float64
    

    【讨论】:

    • 它有效,非常感谢!作为一个附带问题,我可以使用np.stack(s[mask]) 代替np.array(s[mask].tolist() 吗?两者似乎都返回相同的东西,但也许有什么特殊之处?
    猜你喜欢
    • 2018-08-21
    • 2022-06-29
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多