मेरे पास इस तरह की एक श्रृंखला है:

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 लौट रहा हूं, इससे कोई फर्क नहीं पड़ता कि सूचकांक सरणी के अंतिम तत्व पर क्या मूल्य है। मैं इसे कैसे पूरा कर सकता हूं?

कृपया ध्यान दें कि मैं केवल एक श्रृंखला के अंतिम तत्व को नहीं हटा सकता।

0
jakes 8 अप्रैल 2020, 10:39

1 उत्तर

सबसे बढ़िया उत्तर

आप Series के लापता मानों को हटाकर पिछले answer को संशोधित कर सकते हैं और अंत में उन्हें 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:

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
1
jezrael 9 अप्रैल 2020, 05:54