Context: I'd like to create a dictionary with "Variable:values" pairs. The variable has a multindex like this:
Sexo
Masculino Feminino
0 4245.0 4620.0
1 4274.0 4655.0
2 4304.5 4689.5
3 4322.0 4708.5
4 4318.0 4717.5
5 4288.0 4710.5
6 4247.5 4683.5
7 4224.5 4650.5
8 4223.5 4613.5
9 4191.0 4567.0
10 4133.7 4546.9
11 4093.6 4550.1
12 4079.0 4551.5
13 4070.5 4562.6
And I'm creating the dictionary using a dict comprehension:
dict_boxplot = {col:val for (col,val) in zip(subcolumns,df_var.iloc[:,1])}
Where subcolumns
are simply the names "Masculino" and "Feminino". How cold I make a dictionary with this format : {'Masculino': [value1,value2,value3...], 'Feminino': [value1,value2,value3...]} where value1
, etc are the values from each column?
Thank you in advance!
CodePudding user response:
Iterate only on column names, then collect the column and convert to a list
x = {col: df[col].to_list() for col in ['Masculino', 'Feminino']}
Or use DataFrame.to_dict
to help you
x = {k: list(column.values()) for k, column in df.to_dict().items()}
Helper code
import pandas as pd
values = {'Masculino': [4245.0, 4274.0, 4304.5, 4322.0, 4318.0, 4288.0, 4247.5,
4224.5, 4223.5, 4191.0, 4133.7, 4093.6, 4079.0, 4070.5],
'Feminino': [4620.0, 4655.0, 4689.5, 4708.5, 4717.5, 4710.5, 4683.5,
4650.5, 4613.5, 4567.0, 4546.9, 4550.1, 4551.5, 4562.6]}
df = pd.DataFrame(values)
x = {col: df[col].to_list() for col in ['Masculino', 'Feminino']}
print(x) # {'Masculino': [4245.0, ..., 4070.5], 'Feminino': [4620.0, ..., 4562.6]}
x = {k: list(column.values()) for k, column in df.to_dict().items()}
print(x) # {'Masculino': [4245.0, ..., 4070.5], 'Feminino': [4620.0, ..., 4562.6]}
CodePudding user response:
Simply use to_dict('list')
:
df.to_dict('list')
Output:
{'Masculino': [4245.0, 4274.0, 4304.5, 4322.0, 4318.0, 4288.0, 4247.5, 4224.5, 4223.5, 4191.0, 4133.7, 4093.6, 4079.0, 4070.5],
'Feminino': [4620.0, 4655.0, 4689.5, 4708.5, 4717.5, 4710.5, 4683.5, 4650.5, 4613.5, 4567.0, 4546.9, 4550.1, 4551.5, 4562.6]
}