简体   繁体   中英

Creating a dictionary with multiple values per key from specific columns in a dataframe

email_ad                 Name       Manager_Name   Manager_Band_level 
example_email@gmail.com. Tom Banks  Boss1          30
sample_email@gmail.com.  Bill Bob   Boss2          40

How do I create a dictionary with email_ad as the key and each of the other columns in my data frame as values. I tried this

mydict={df['email_ad']:[df['Name'],df['Manager Name'], df['Manager_Band_level']]}

This did not work. I have been stuck forever on this. Any help would be amazing!

Use zip with dict as:

mydict = dict(zip(df['email_ad'], df.iloc[:, 1:].to_numpy().tolist()))

mydict
{'example_email@gmail.com.': ['Tom Banks', 'Boss1', 30],
 'sample_email@gmail.com.': ['Bill Bob', 'Boss2', 40]}

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