I have a DataFrame of lists and would like to pick 6 random items out of the list in a row, every picked item should be saved in the same row but in a different column.
DataFrame:
id column_with_lists
1 ['10A','11B','12C','13D','14E','15F','16G','17H','18I']
2 ['34X','35Y','46Z','48A','49B','50C','51D']
3 ['232H', '23Q', '26W', '89D', '328A', '219C', '432G', '324A']
Desired result:
id col col1 col2 col3 col4 col5
1 '10A' '14E' '11B' '18I' '17H' '13D'
2 '46Z' '48A' '49B' '50C' '51D' '34X'
3 '232H' '26W' '89D' '328A' '432G' '324A'
Edit:
My DataFrame contains two columns, the first column ist the id
and the second one the lists of ids column_with_lists
. My goal is to get 6 random ids from the list within column_with_lists
. Every picked id should be saved in the same row but different column of a dataframe
I was thinking about something like:
['col'] = df.apply( lambda x: random.sample( x['column_with_lists'], 1), axis=1)
but for multiple columns.