大熊猫列读取和添加列

时间:2020-01-07 19:37:29

标签: python python-3.x pandas

当前,在python中使用pandas来加载大型CSV文件。我正在努力根据数据框中三列中的多个值有效地创建和添加新列。

共有三列(时间,二氧化碳和成本),我想根据一些计算添加一个新的列,称为gcost。

下面的代码可以工作,但是非常慢。我相信是row['time']导致它变慢的原因:

输入

Id,time,CO2eq,cost

0,10,10,10

1,5,5,5

2,2,3,6

预期结果

Id,time,CO2eq,cost,gcost

0,10,10,10,X

1,5,5,5,X

2,2,3,6,X  


代码

#wftime, wfco2eq and wfcost are inputted from the front-end.
    hhinfo_input_df = pd.read_csv(input_file_path, header=0,
                              names=['Id','CO2eq', 'time', 'cost'])

    hhinfo_input_df['gcost'] = hhinfo_input_df.apply(cost_generate, axis=1)
    return hhinfo_input_df

#Normalized weighted values of each criterion (input by user)
def cost_generate(row):
    Norm_time = (row['time'] * (wftime / max_time)) * 100000
    Norm_co2eq = (row['CO2eq'] * (wfco2eq / max_co2eq)) * 100000
    Norm_cost = (row['cost'] * (wfcost / max_cost)) * 100000

    gcost = int(round(Norm_time)) + int(round(Norm_co2eq)) + int(round(Norm_cost))

    #gcost should never be 0.
    if gcost == 0:
        return 1
    return gcost

2 个答案:

答案 0 :(得分:1)

无需在行级别执行这些操作。如果您仅使用这些操作的矢量化版本,熊猫将更快地处理该问题:

df = pd.read_csv(input_file_path, header=0,
                 names=['Id','CO2eq', 'time', 'cost'])

Norm_time = (df['time'] * (wftime / max_time)) * 100000
Norm_co2eq = (df['CO2eq'] * (wfco2eq / max_co2eq)) * 100000
Norm_cost = (df['cost'] * (wfcost / max_cost)) * 100000
df["gcost"] = Norm_time.round().astype(int) + Norm_co2eq.round().astype(int) + Norm_cost.round().astype(int)

答案 1 :(得分:0)

您可以一次使用所有公式吗?

示例:

import pandas as pd

data = [ ['A',2,1], ['B',1,3] ]

dataset = pd.DataFrame(data,columns= ['ID','Item1','Item2']

dataset['total'] = dataset['Item1'] + dataset['Item2']