Issue
I am trying to create a dictionary using pandas
DataFrame.
import pandas as pd
df = pd.DataFrame({'name': ["p1","p1","p2","p2","p2"],
'val': [0, 1, np.nan, 1, 0] })
I'd like to create a dictionary with name
column as key
and val
column as values
. Values will be a list. I am using a for loop
as the DataFrame is only 100 rows.
mydict = dict()
valList = []
for index, row in df.iterrows():
# create list of values
......
mydict[row['name']] = valList
Expected output:
{
'p1': [0, 1],
'p2': [nan, 1, 0]
}
Open to other approaches, but I have a slight preference for using for loop and iterrows()
.
Solution
You could use groupby
and a dictionary comprehension:
d = {k:list(v) for k,v in df.groupby('name')['val']}
output:
{'p1': [0.0, 1.0], 'p2': [nan, 1.0, 0.0]}
using iterrows (not my favorite option)
NB. this will be quite slower on large dataframes
from collections import defaultdict
d = defaultdict(list)
for _, row in df.iterrows():
d[row['name']].append(row['val'])
dict(d)
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.