Skip to content Skip to sidebar Skip to footer

Summing Up The Total Based On The Random Number Of Inputs Of A Column

I need to sum up the 'value' column amount for each value of col1 of the File1 and export it to an output file. I'm new in python and need to do it for thousands of records. File1

Solution 1:

You can use pandas for this:

df = pd.read_csv('in.csv', delim_whitespace=True)

#      col1      col2       value# 559     1  91987224  2400000000# 559     0  91987224   100000000# 558     0  91987224   100000000# 557     2  87978332   500000000# 557     1  59966218  2400000000# 557     0  64064811   100000000

result = df.groupby(df.index)['value'].sum().reset_index()

#    index       value# 0    557  3000000000# 1    558   100000000# 2    559  2500000000

result.to_csv('out.csv', index=False)

Solution 2:

Use read_csv for create DataFrame, then groupby by index by level=0 and aggregate sum. Last export to_csv:

df = pd.read_csv(file1)
df.groupby(level=0)['value'].sum().to_file(file2)

Post a Comment for "Summing Up The Total Based On The Random Number Of Inputs Of A Column"