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

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

將 lambda 與 pandas 一起使用以計算以現有列為條件的新列

將 lambda 與 pandas 一起使用以計算以現有列為條件的新列

Smart貓小萌 2023-06-13 17:20:32
我需要在 pandas DataFrame 中創建一個新列,該列計算為 DataFrame 中 2 個現有列的比率。但是,比率計算中的分母將根據在 DataFrame 的另一列中找到的字符串值而變化。例子。示例數據集:import pandas as pd df = pd.DataFrame(data={'hand'      : ['left','left','both','both'],                          'exp_force' : [25,28,82,84],                          'left_max'  : [38,38,38,38],                          'both_max'  : [90,90,90,90]})我需要df['ratio']根據 的條件創建一個新的 DataFrame 列df['hand']。如果df['hand']=='left'那么df['ratio'] = df['exp_force'] / df['left_max']如果df['hand']=='both'那么df['ratio'] = df['exp_force'] / df['both_max']
查看完整描述

3 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以使用np.where():


import pandas as pd

df = pd.DataFrame(data={'hand'      : ['left','left','both','both'], 

                        'exp_force' : [25,28,82,84], 

                        'left_max'  : [38,38,38,38], 

                        'both_max'  : [90,90,90,90]})

df['ratio'] = np.where((df['hand']=='left'), df['exp_force'] / df['left_max'], df['exp_force'] / df['both_max'])

df


Out[42]: 

   hand  exp_force  left_max  both_max     ratio

0  left         25        38        90  0.657895

1  left         28        38        90  0.736842

2  both         82        38        90  0.911111

3  both         84        38        90  0.933333

或者,在現實生活中,如果您有很多條件和結果,那么您可以使用np.select(),這樣您就不必np.where()像我在舊代碼中所做的那樣不斷重復您的語句。最好np.select在這些情況下使用:


import pandas as pd

df = pd.DataFrame(data={'hand'      : ['left','left','both','both'], 

                        'exp_force' : [25,28,82,84], 

                        'left_max'  : [38,38,38,38], 

                        'both_max'  : [90,90,90,90]})

c1 = (df['hand']=='left')

c2 = (df['hand']=='both')

r1 = df['exp_force'] / df['left_max']

r2 = df['exp_force'] / df['both_max']

conditions = [c1,c2]

results = [r1,r2]

df['ratio'] = np.select(conditions,results)

df

Out[430]: 

   hand  exp_force  left_max  both_max     ratio

0  left         25        38        90  0.657895

1  left         28        38        90  0.736842

2  both         82        38        90  0.911111

3  both         84        38        90  0.933333


查看完整回答
反對 回復 2023-06-13
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

枚舉


for i,e in enumerate(df['hand']):

 

  if e == 'left':

    df.at[i,'ratio'] = df.at[i,'exp_force'] / df.at[i,'left_max']

  if e == 'both':

    df.at[i,'ratio'] = df.at[i,'exp_force'] / df.at[i,'both_max']

df

輸出:


    hand    exp_force   left_max    both_max    ratio

0   left    25            38          90      0.657895

1   left    28            38          90      0.736842

2   both    82            38          90      0.911111

3   both    84            38          90      0.933333


查看完整回答
反對 回復 2023-06-13
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您可以使用apply()數據框的方法:


df['ratio'] = df.apply(

    lambda x: x['exp_force'] / x['left_max'] if x['hand']=='left' else x['exp_force'] / x['both_max'],

    axis=1

)


查看完整回答
反對 回復 2023-06-13
  • 3 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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