简体   繁体   中英

How to add a value inside an numpy array to a python dictionary

i'm trying to loop through a pandas dataframe's columns (which consists of 1's and 0's) to groupby and sum another column then add the groupby column name to an empty dictionary as a key and the summed value as the value. But my current code adds an array as the value instead of the actual value. Here is some sample code below.

import pandas
sample_dict = {'flag1':[0,1,1,1,1,0],
               'flag2':[1,1,1,0,0,1],
               'flag3':[0,0,0,0,0,1],
               'flag4':[1,1,1,1,0,0],
               'flag5':[1,0,1,0,1,0],
               'dollars':[100,200,300,400,500,600]}
sample_df = pd.DataFrame(sample_dict)

ecols = sample_df.columns[:5]
rate = .46
empty_dict = {}
for i in ecols:
    df= sample_df[sample_df[i] == 1]
    yield1 = df.groupby(i)['dollars'].sum().values*rate
    empty_dict[i] = yield1
    
empty_dict

That code yields the following output:

Out[223]: 
{'flag1': array([644.]),
 'flag2': array([552.]),
 'flag3': array([276.]),
 'flag4': array([460.]),
 'flag5': array([414.])}

I would just like to have the actual integer as the value and not the array.

You consistently get an array of one single element: just take its first element (if it has one):

...
empty_dict[i] = yield1[0] if len(yield) >=1 else np.nan
...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM