Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17360

How to groupby(col1) and sum(col2), (col3), (col 4)

$
0
0

I want to make a function that creates a data table of the sum of each indicator for each site.I need to group by the site, as there are 100 Devons, 70 Cornwalls and so on, so that Site is the dependent variable, and Devon reads to have A = 6, B = 4, C = 2, D = 1.

I have another two column data table which reads site occurence (e.g Devon 2) and will find the average indicators per site later.

| Site     |     A    |     B    |    C     |    D     || -------- | -------- | ---------| -------- | -------- |      | Cornwall |     0    |     1    |    0     |    3     || Devon    |     1    |     4    |    1     |    0     || Devon    |     5    |     0    |    1     |    1     |

I have tried a few methods, but only get errors. I tried asking AI for the forst time but got this. Still errors.

def CHEGD_funct(df, group_col, sum_cols):    grouped_sum_df = df.groupby(group_col).sum([sum_cols]).reset_index()    return grouped_sum_df    dataframe = pd.read_csv('Grassland_Fungi.csv', low_memory=False)    sitecolumn = dataframe.iloc[:,6]     (Cleaned site column into site list)    site_col = pd.DataFrame(list(sitelist))    A = dataframe.iloc[:,11]    B = dataframe.iloc[:,12]    C = dataframe.iloc[:,13]    D = dataframe.iloc[:,14]    ABCD = (A, B, C, D)functionrun = CHEGD_funct(dataframe, site_col, ABCD)print(functionrun)

Any recommendations?


Viewing all articles
Browse latest Browse all 17360

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>