Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据一列输入的随机数求和总数_Python_Calculated Columns_Data Processing - Fatal编程技术网

Python 根据一列输入的随机数求和总数

Python 根据一列输入的随机数求和总数,python,calculated-columns,data-processing,Python,Calculated Columns,Data Processing,我需要对File1的col1的每个值的“value”列amount求和,并将其导出到输出文件。我是python新手,需要对数千条记录执行此操作 文件1 期望输出: 提前谢谢 由于权限问题,我无法使用pandas库。我尝试了以下代码。与跟踪共享它: import csv fin = open("File1.txt","r") list_txid = {} num_tx = {} amount_tx = {} for line in fin: line = line.rstrip()

我需要对File1的col1的每个值的“value”列amount求和,并将其导出到输出文件。我是python新手,需要对数千条记录执行此操作

文件1 期望输出: 提前谢谢

由于权限问题,我无法使用pandas库。我尝试了以下代码。与跟踪共享它:

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] += 1
    amount_tx[txid] += amount
    print("{0}\t{1:d}\t{2:d}".format(txid, amount_tx[txid]))
回溯: 回溯(最近一次呼叫最后一次): 文件“C:\Users….\sum.py”,第14行,在 金额=整数(f[3])
索引器:列出超出范围的索引

您可以为此使用
pandas

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)
用于创建
DataFrame
,然后按
level=0
索引和聚合
sum
。上次导出:


请告诉我们您到目前为止做了什么,以及与回溯共享的代码有什么问题,tnx-@Slam@MikeScotty…请再次检查我的问题。我按照要求进行了编辑。好心的,把否决票从帖子上拿开。tnx提前取消了我的否决票。谢谢,但不幸的是我不能用熊猫。我已经尝试编辑了这个问题,如果您能在这方面指导我,我将不胜感激@很遗憾,我不能用熊猫。我已经尝试编辑了这个问题,如果您能在这方面指导我,我将不胜感激。
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] += 1
    amount_tx[txid] += amount
    print("{0}\t{1:d}\t{2:d}".format(txid, amount_tx[txid]))
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)
df = pd.read_csv(file1)
df.groupby(level=0)['value'].sum().to_file(file2)