How to put elements of a pandas index as values of a dictionary?
Question:
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) ?
Answers:
Do this:
import pandas as pd
data = {'Index_col': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8'],
'col1': ['F1-R1', 'F1-R1', 'F1-R1', 'F1-R1', 'F1-R2', 'F1-R2', 'F1-R2', 'F1-R2']}
df = pd.DataFrame(data)
dic = {key: [] for key in df.col1.unique()}
for idx, row in df.iterrows():
dic[row["col1"]].append(row["Index_col"])
print(dic)
which gives
{'F1-R1': ['P1', 'P2', 'P3', 'P4'], 'F1-R2': ['P5', 'P6', 'P7', 'P8']}
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']}
Another possible solution. which uses dictionary comprehension
:
{x: df.loc[df['col1'].eq(x),'Index_col'].to_list() for x in df['col1'].unique()}
Output:
{'F1-R1': ['P1', 'P2', 'P3', 'P4'], 'F1-R2': ['P5', 'P6', 'P7', 'P8']}
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) ?
Do this:
import pandas as pd
data = {'Index_col': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8'],
'col1': ['F1-R1', 'F1-R1', 'F1-R1', 'F1-R1', 'F1-R2', 'F1-R2', 'F1-R2', 'F1-R2']}
df = pd.DataFrame(data)
dic = {key: [] for key in df.col1.unique()}
for idx, row in df.iterrows():
dic[row["col1"]].append(row["Index_col"])
print(dic)
which gives
{'F1-R1': ['P1', 'P2', 'P3', 'P4'], 'F1-R2': ['P5', 'P6', 'P7', 'P8']}
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']}
Another possible solution. which uses dictionary comprehension
:
{x: df.loc[df['col1'].eq(x),'Index_col'].to_list() for x in df['col1'].unique()}
Output:
{'F1-R1': ['P1', 'P2', 'P3', 'P4'], 'F1-R2': ['P5', 'P6', 'P7', 'P8']}