Summing up the total based on the random number of inputs of a column

2024/7/6 21:20:56

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

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

Desired Output:

col1      Sum 
559     2500000000
558     1000000000
557     3000000000    

Thanks in advance.

P.S : I can't use the pandas library due to permission issues.I tried the following code. Sharing it with trace backs:

import csv 
fin = open("File1.txt","r")
list_txid = {}
num_tx = {}
amount_tx = {}for line in fin:line = line.rstrip()f = line.split("\t")txid = f[0]amount = int(f[3])fin.close()
for txid in list_txid:num_tx[txid] += 1amount_tx[txid] += amountprint("{0}\t{1:d}\t{2:d}".format(txid, amount_tx[txid]))

Traceback :

Traceback (most recent call last):File "C:\Users....\sum.py", line 14, in amount = int(f[3]) IndexError: list index out of range

Answer

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   100000000result = df.groupby(df.index)['value'].sum().reset_index()#    index       value
# 0    557  3000000000
# 1    558   100000000
# 2    559  2500000000result.to_csv('out.csv', index=False)
https://en.xdnf.cn/q/119841.html

Related Q&A

What is wrong with this Binomial Tree Backwards Induction European Call Option Pricing Function?

The function below works perfectly and only needs one thing: Removal of the for loop that creates the 1000 element array arr. Can you help me get rid of that for loop? Code is below #Test with europea…

Regex behaving weird when finding floating point strings [duplicate]

This question already has answers here:re.findall behaves weird(3 answers)Closed 4 years ago.So doing this (in python 3.7.3):>>> from re import findall >>> s = 7.95 + 10 pieces >&g…

how do i get this code to tell me what position a word in the sentence is

varSentence = ("The fat cat sat on the mat")print (varSentence)varWord = input("Enter word ")varSplit = varSentence.split()if varWord in varSplit:print ("Found word") else…

Reverse geocoding with Python/GoogleMaps API: How to parse response

Im attempting to use the Google Maps Geocoding API to find the State associated with a latitude and longitude with a Python script. I have no trouble getting back a response from the server, but when …

How to constantly generate random numbers inside for i in range loop

import random world_size=8 for i in range(world_size)chunk_type=random.randint(1,2)print(chunk_type)import randomclass chunk_type():chunk_type=random.randint(1,2)world_size=8for i in range(world_size):…

How to count IDs that have a given combination of flags?

I have dataframe like that. I need to choose and count all distinct users, who have title "banner_click" and "order". So I dont understand how to do it in pandas, in SQL you do like…

How to calculate quarterly wise churn and retention rate using python

How to calculate quarterly wise churn and retention rate with date column using python. with date column i want to group that quarterly using python.This is used to calculate the churn count groupby qu…

splitting a list into two lists based on a unique value

I have a text file that looks something like this:hello 12 hello 56 world 25 world 26Is there a way in python that I can somehow parse the list that I obtain from reading this data to obtain two separa…

Customize axes in Matplotlib

I am a beginner with Python, Pandas and Matplotlib. I would like to customize the entries at the axes of a scatter plot. I have the following data:So on the x-axis there should be 5 entries, with the f…

how to show the max and min from user input?

Nevermindif xmin == 1:print(ymin)I tried using the max and min but I get a typeerror which is int object not iterable.