Answers for "pandas sample weights example"

0

pandas sample weights example

# Assuming you have a column with few category and you want to see more/few
# of some cateogry in your sample, then you can try the following code

# remember the weights must adds up to 1
                |____________________________________
                                      ↓      ↓      ↓
w = df['label'].apply( lambda x: {-1:0.5, 0:0.4, 1:0.1}[x] )
df.sample(n=1, weights=w, axis=0)
Posted by: Guest on April-18-2022

Python Answers by Framework

Browse Popular Code Answers by Language