I have following data. I need to form a dictionary from this. There are 20 columns with y1_bin, y2_bin, .....y20_bin. In this toy data, I have shown three columns only.
Firm y1 y2 y3 prob_y1 prob_y2 prob_y3 y1_bin y2_bin y3_bin
0 A 1 2 7 0.006897 0.000421 0.002729 binA binA binB
1 B 2 3 45 0.013793 0.000632 0.017544 binA binA binE
2 C 3 4 40 0.020690 0.000842 0.015595 binA binA binE
3 D 4 7 3 0.027586 0.001474 0.001170 binA binB binA
4 E 5 9 4 0.034483 0.001895 0.001559 binB binB binA
5 F 6 400 12 0.041379 0.084211 0.004678 binB binH binC
6 G 7 50 32 0.048276 0.010526 0.012476 binB binF binE
7 H 8 70 0 0.055172 0.014737 0.000000 binB binF binA
8 I 9 95 76 0.062069 0.020000 0.029630 binB binF binF
9 J 10 98 1 0.068966 0.020632 0.000390 binC binF binA
10 K 20 2 45 0.137931 0.000421 0.017544 binD binA binE
11 L 30 10 2000 0.206897 0.002105 0.779727 binE binC binH
12 M 40 4000 300 0.275862 0.842105 0.116959 binE binH binH
I have written following syntax to append the keys and values:
from collections import defaultdict
mydict = defaultdict(list)
for k, v, m,j in zip(df33.Firm.values, df33.y2_bin.values, df33.y1_bin.values, df33.y3_bin.values):
mydict[k].append(v)
mydict[k].append(m)
mydict[k].append(j)
print(mydict)
This is expected result (which I can get from above for loop- and i know this is not the most efficient way to write code.). Is there a better way to make this efficient so that I do not have to keep adding df33.***.values
and mydict[k].append(****)
in the for loop.
defaultdict(<type 'list'>, {'A': ['binA', 'binA', 'binB'], 'C': ['binA', 'binA', 'binE'], 'B': ['binA', 'binA', 'binE'], 'E': ['binB', 'binB', 'binA'], 'D': ['binB', 'binA', 'binA'], 'G': ['binF', 'binB', 'binE'], 'F': ['binH', 'binB', 'binC'], 'I': ['binF', 'binB', 'binF'], 'H': ['binF', 'binB', 'binA'], 'K': ['binA', 'binD', 'binE'], 'J': ['binF', 'binC', 'binA'], 'M': ['binH', 'binE', 'binH'], 'L': ['binC', 'binE', 'binH']})
You could try something like this:
df = pandas.DataFrame([
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
...
])
my_dict = dict(zip(df.Firm, [list(df.get(f'y{idx}_bin').values) for idx in range(1, 31)]))
#Output
{'A': ['binA', 'binA', 'binA', 'binA'],
'B': ['binB', 'binB', 'binB', 'binB']
...
}
How about using DataFrame.to_dict
method with a list comprehension as follows:
import pandas as pd
df = pd.DataFrame([
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'A', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
{'Firm': 'B', 'y1_bin': 'binA', 'y2_bin': 'binA', 'y3_bin': 'binB'},
])
# set column 'Firm' as the index (needed for df.to_dict() to work)
df.set_index('Firm', inplace=True)
my_dict = {k: list(v.values()) for k, v in df.to_dict('index').items()}
# output:
{'A': ['binA', 'binA', 'binB'], 'B': ['binA', 'binA', 'binB']}
Take a look at to_dict for more info on its output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.