亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過將每一行轉換為熊貓數據框中的字典來創建新列

通過將每一行轉換為熊貓數據框中的字典來創建新列

喵喔喔 2022-11-01 15:11:53
所以我從以下 csv 創建了一個 pandas 數據框:id  age00   education   marital gender  ethnic  industry    income000   51.965         17         0      1       0         5    761101   41.807         12         1      0       0         1    432162   36.331         12         1      0       1         3    521183   56.758          9         1      1       2         2    47770我的目標是創建一個名為future_income的新列,它獲取每一行并使用我的模型計算未來收入。這是由我在下面創建的類中的predictFinalIncome變量完成的:class myModel:  def __init__(self, bias) :    self.bias = bias # bias is a dictionary with info to set bias on the gender function and the ethnic function  def b_gender(self, gender):    effect = 0    if (self.bias["gender"]): # if there is gender bias in this model/world (from the constructor)       effect = -0.0005 if (gender<1) else 0.0005  # This amount to 1.2% difference annually    return self.scale * effect  def b_ethnic(self, ethnic):    effect = 0    if (self.bias["ethnic"]): # if there is ethnic bias in this model/world (from the constructor)       effect = -0.0007 if (ethnic < 1) else -0.0003 if (ethnic < 2) else 0.0005     return self.scale * effect  # other methods/functions  def predictGrowthFactor( self, person ): # edited    factor = 1 + person['education'] + person['marital'] + person['income'] + person['industry']    return factor  def predictIncome( self, person ): # perdict the new income one MONTH later. (At least on average, each month the income grows.)    return person['income']*self.predictGrowthFactor( person )  def predictFinalIncome( self, n, person ):     n_income = self.predictIncome( person )    for i in range(n):       n_income = n_income * i    return n_income在這種情況下,n 是 120。所以簡而言之。我想取出每一行,將其放入名為predictFinalIncome的類函數中,并在我的 df 上有一個名為 future_income 的新變量,這是他們在 120 個月內的收入。
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

我認為你只是讓它變得非常復雜,你所做的所有計算實際上都可以通過一個函數來完成,除非你需要你的中間結果用于其他用途。


您可以創建一個可應用于數據框每一行的函數:


def predictFinalIncome(row, n):

    factor = 1 + row['education'] + row['marital'] + row['income'] + row['industry']

    n_income = row['income'] * factor

    for i in range(n):

        n_income = n_income * i

    return n_income

然后,使用df.apply:


df.apply(lambda r: predictFinalIncome(r, 120), axis=1)

它返回 0,因為當你這樣做時for i in range(n),你實際上是從 0 開始的,所以結果總是 0。你需要修復它。


更新:使函數存在于Model類中


從您的帖子中,我沒有看到此函數存在于模型中的明顯原因,特別是此函數不使用任何其他方法,也沒有使用您創建的偏差屬性,但它就是這樣。


class myModel:

    def __init__(self, bias) :

        self.bias = bias


    def predictFinalIncome(self, row, n):

        factor = 1 + row['education'] + row['marital'] + row['income'] + row['industry']

        n_income = row['income'] * factor

        for i in range(n):

            n_income = n_income * i

        return n_income


# to use:

model = myModel(bias)

df.apply(lambda r: model.predictFinalIncome(r, 120), axis=1)


查看完整回答
反對 回復 2022-11-01
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號