目前正在使用.apply()函数执行Pandas操作。

fund_table[fund_table.fund_class == 'EQ']['fund_weight'].apply(lambda x: ((x*overall_wts[1])/100))

fund_table[fund_table.fund_class == 'DB']['fund_weight'].apply(lambda x: ((x*overall_wts[0])/100))

fund_table[fund_table.fund_class == 'LQ']['fund_weight'].apply(lambda x: ((x*overall_wts[2])/100))


每个代码都在修改某些行集合,现在如何更新主数据框,

我尝试过这样的事情:

fund_table['fund_weight'] = fund_table[fund_table.fund_class == 'EQ']['fund_weight'].apply(lambda x: ((x*overall_wts[1])/100))
fund_table['fund_weight'] = fund_table[fund_table.fund_class == 'DB']['fund_weight'].apply(lambda x: ((x*overall_wts[0])/100))
fund_table['fund_weight'] = fund_table[fund_table.fund_class == 'LQ']['fund_weight'].apply(lambda x: ((x*overall_wts[2])/100))


但失败了,“ fund_weight”列的所有值都更改为Nan

正确的做法是什么?

最佳答案

分配给fund_weight时,将覆盖先前保留的所有列,因此下一行将使用错误的数据。

此外,当您根据fund_class进行过滤时,会创建一个较小的数据框。 fund_table[fund_table.fund_class == 'EQ']['fund_weight']小于fund_table,因此您的apply产生的系列较小。当您尝试将此系列分配给整个数据框时,pandas用NaN填充缺失值。

结果,除了fund_weight等于'EQ'的行之外,第一行将fund_class的每一行都转换为NaN。您的下一行将过滤fund_class等于'EQ'的所有行,因此它只能看到NaN值,而现在fund_weight的所有行都是NaN。

您想要更多类似的东西:

def calc_new_weight(row):
    if row['fund_class'] == 'EQ':
        overall_wt = overall_wts[1]
    elif row['fund_class'] == 'DB':
        overall_wt = overall_wts[0]
    elif row['fund_class'] == 'LQ':
        overall_wt = overall_wts[2]
    return row['fund_weight'] * overall_wt / 100
fund_table['fund_weight_calc'] = fund_table.apply(calc_new_weight, axis=1)

关于python - pandas lambda操作后更新数据框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47098429/

10-12 22:09
查看更多