简体   繁体   中英

Add elements from list to new data frame column based on condition

I have this dataframe:

id1 id2
2341 qw123
2321 -
- de121
2341 qd111

And I want to add 3rd column id3 with randomly generated ids in a list:

['11231', '123141', '234512']

The thing that makes it difficult to me is how to attach the same random id from the list to each row where id1 is the same.

For example the output file should look like this:

id1 id2 id3
2341 qw123 11231
2321 - 123141
- de121 234512
2341 qd111 11231

Any solution is appreciated!

You can create a dict for mapping the unique id1 keys to the random numbers. Then use .map() to map id1 values to these random numbers for assignment to new column id3 , as follows:

num_list = ['11231', '123141', '234512']
id1_unique = df['id1'].unique()

m_dict = dict(zip(id1_unique,  np.random.choice(num_list, len(id1_unique))))

df['id3'] = df['id1'].map(m_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM