1

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.

1

1 Answer 1

1

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
...
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you but I'm getting this: Traceback (most recent call last): File "<ipython-input-232-949311df9ecd>", line 9, in <module> edit_dict[i] = prem[0] IndexError: index 0 is out of bounds for axis 0 with size 0
@Jordan Could your original code give empty arrays?
Yes it can have a empty value.
Thanks @SergeBallesta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.