Let's say I have students infos id
, age
and class
as follows:
id age class
0 1 23 a
1 2 24 a
2 3 25 b
3 4 22 b
4 5 16 c
5 6 16 d
I want to groupby class
and create a new column named major
by randomly assign math, art, business, science
into it, which means for same class, the major strings are same.
We may need to use apply(lambda x: random.choice..)
to realize this, but I don't know how to do this. Thanks for your help.
Output expected:
id age major class
0 1 23 art a
1 2 24 art a
2 3 25 science b
3 4 22 science b
4 5 16 business c
5 6 16 math d