lundi 6 avril 2020

Add a random sample column to a dataframe

Say I have a table like so:

| Name   | Age |
|--------|-----|
| Bob    | 2   |
| John   | 3   |
| Tim    | 4   |
| Ben    | 5   |
| Ella   | 4   |
| Sophie | 5   |
| Grace  | 6   |
| Bill   | 34  |
| Ron    | 23  |
| Harry  | 2   |

How could I add a new column which selects a random 10% of the rows and adds a new column with True? Then sets the rest to False. Like so?

| Name   | Age |       |
|--------|-----|-------|
| Bob    | 2   | False |
| John   | 3   | False |
| Tim    | 4   | False |
| Ben    | 5   | True  |
| Ella   | 4   | False |
| Sophie | 5   | False |
| Grace  | 6   | False |
| Bill   | 34  | False |
| Ron    | 23  | False |
| Harry  | 2   | False |



Aucun commentaire:

Enregistrer un commentaire