'I am trying to apply a defined function to a grouped pandas grouped dataframe and output the results to a csv

I have a defined function that requires a list, and the function outputs one value for every item in the list. I need to group by industry code (SIC), and apply the function within industry (so only industry 1 firms are grouped together for the defined calculation). example:

1       50
1       40
2       100
2       110

I do the following code:

dr=pd.read_csv("sample.csv", usecols=columns)
d1=dr.groupby('SIC')['value'].apply(list)
for groups in d1
    a=my_function(groups)
    b=pd.DataFrame(A)
    b.to_csv('output.csv', index=False)

I expected to get an output file with the function values for all 4 rows (lets say I want the difference between that row and the average. row 1 should be 50-(avg(50+40)) which equals 5.

Instead, I get a csv file with only the last group's values. It seems like I should make a new CSV file for each group, but by doing the apply(list) I cant figure out how to identify each group. Edit: I modified the functionality as described in the comment below to output only one file.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source