使用df.at时覆盖列值

问题描述 投票:1回答:2

我写了一个函数,它读入一个csv文件,执行一些计算并将输出写入同一个文件。要将计算值附加到新列,我使用的是df.at[index, column_name] = value

这是我的代码

def total_calc(n):
     input = pd.read_csv(file_name)
     input['calc'] = 0.0
     for index, row in input.iterrows():

       # perform calculations

     input.at[index, 'calc'] = calc_value
     input.to_csv(file_name, index=False)

当我将函数用于n的多个值时,它将在同一列中写入值,覆盖数据帧中先前n个值的值。我尝试在函数中使用i并给出index+i,如下所示:

def total_calc(i,n):
     input = pd.read_csv(file_name)
     input['calc'] = 0.0
     for index, row in input.iterrows():

       # perform calculations

     input.at[index+i, 'calc'] = calc_value
     input.to_csv(file_name, index=False)

total_calc(1,2)
total_calc(2,8)

但是,列值仍会被覆盖。有没有办法将函数中多个值的列写入同一文件而不覆盖?

所以这些是我当前的数据集列

names values wickets score

运行所有必需的功能后我需要这个

names values wickets score calc calc1 calc2
python python-3.x pandas
2个回答
1
投票

我认为你需要通过range循环并将值k添加到列名 - 每个循环都创建另一列:

def total_calc(i,n):

     for k in range(n):
         input = pd.read_csv(file_name)
         input['calc' + str(i)] = 0.0
         for index, row in input.iterrows():

           # perform calculations

         input.at[index, 'calc' + str(i)] = calc_value

     input.to_csv(file_name, index=False)

0
投票

因此,我的代码中的一次更改就得到了我所需要的答案。我把输入文件放在循环之外就可以了!

input = pd.read_csv(file_name)
def total_calc(i,n):
     input['calc'] = 0.0
     for index, row in input.iterrows():

       # perform calculations

     input.at[index+i, 'calc'] = calc_value
     input.to_csv(file_name, index=False)

total_calc(1,2)
total_calc(2,8)

如果要添加大量列,但由于我只需要三个,上面的答案很有用,这个答案对我来说很好。

© www.soinside.com 2019 - 2024. All rights reserved.