I am currently importing a file as so:
df= pd.read_csv(r"Test.csv")
And the output looks like
Type Value
0 Food_Place_1 1
1 Food_Place_2 2
2 Car_Type_1 3
3 Car_Type_2 4
I would like to iterate through this df and depending on the Type column allocated to a dictionary like this
food_type_dict = {'Type': ['Food_Place_1', 'Food_Place_2'], 'Value': [1, 2]}
car_type_dict = {'Type': ['Car_Type_1', 'Car_Type_2'], 'Value': [3, 4]}
My plan was to convert the entire dataframe into a dictionary and filter from there. However when I try to convert using this, the output is not what I was expecting. I can't seem to remove the Value header from the dictionary
df_dict = df.set_index(['Type']).T.to_dict('dict')
Output
{'A1': {'Value': 1},....}
CodePudding user response:
Create category for possible aggregate lists for nested dictionary:
#If category is set by remove digits
cat = df['Type'].str.replace('\d','')
#If category is set by first letter
#cat = df['Type'].str[0]
d = df.rename(columns={'Type':'Component'}).groupby(cat).agg(list).to_dict('index')
print (d)
{'A': {'Component': ['A1', 'A2'], 'Value': [1, 2]},
'B': {'Component': ['B1', 'B2'], 'Value': [3, 4]}}
Then instead a_type_dict
use d['A']
, b_type_dict
use d['B']
.
EDIT:
cat = df['Type'].str.split('_').str[0]
d = df.rename(columns={'Type':'Component'}).groupby(cat).agg(list).to_dict('index')
print (d)
{'Car': {'Component': ['Car_Type_1', 'Car_Type_2'], 'Value': [3, 4]},
'Food': {'Component': ['Food_Place_1', 'Food_Place_2'], 'Value': [1, 2]}}