I’m struggling with an error in pandas which is driving me crazy.
I want to build a dictionary which extracts some data from a pandas df:
Index_col col1
P1 F1-R1
P2 F1-R1
P3 F1-R1
P4 F1-R1
P5 F1-R2
P6 F1-R2
P7 F1-R2
P8 F1-R2
(etc)
Would give:
{'F1-R1': ['P1', 'P2', 'P3', 'P4'],
'F1-R2': ['P5', 'P6', 'P7', 'P8']}
However the following code:
dic = dict.fromkeys(df.col1.unique(), [])
for idx, row in df.iterrows():
dic[row["col1"]].append(idx)
Produces
{'F1-R1': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8']],
'F1-R2': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8']}
I can’t figure ut why :/. Does someone has an answer (or another way to proceed) ?
>Solution :
Just group by col1 and collect lists with converting the result into dict:
d = df.groupby('col1')['Index_col'].apply(list).to_dict()
{'F1-R1': ['P1', 'P2', 'P3', 'P4'], 'F1-R2': ['P5', 'P6', 'P7', 'P8']}