建立
fruit = pd.Series(['banana'] * 57 + ['apple'] * 54 + [None] * 10, name='fruit')
运用 pd.Series.sample
nullfruit = fruit.isnull() fruit.loc[nullfruit] = fruit.dropna().sample(nullfruit.sum()).values
使用np.random.choice
和np.unique
nullfruit = fruit.isnull().values u, c = np.unique(fruit.values[~nullfruit], return_counts=1) fruit.loc[nullfruit] = np.random.choice(u, nullfruit.sum(), p=c / c.sum())