第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Pandas 轉(zhuǎn)換:創(chuàng)建具有函數(shù)的兩列

Pandas 轉(zhuǎn)換:創(chuàng)建具有函數(shù)的兩列

縹緲止盈 2021-11-30 10:36:56
我有一個數(shù)據(jù)框 dfdf:GROUP VALUE 1     5 2     2 1     10 2     20 1     7還有一個功能import numpy as npfrom scipy import statsdef z_score(x):   z = np.abs(stats.zscore(x))   c = np.where(x > 5, 1, 0)   return z,c我試圖在函數(shù)輸出和熊貓變換方法的幫助下在數(shù)據(jù)框中創(chuàng)建兩列df['zscore'], df['label'] = a.groupby(['GROUP'])['VALUE'].transform(z_score)但是在運(yùn)行上述代碼段后出現(xiàn)以下錯誤ValueError: Length of passed values is 2, index implies 3如何實現(xiàn)這一目標(biāo)?
查看完整描述

1 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗 獲得超4個贊

您可以DataFrame在函數(shù)中返回:


def z_score(x):

   z = np.abs(stats.zscore(x))

   c = np.where(x > 5, 1, 0)

   return pd.DataFrame({'zscore':z,'label':c}, index=x.index)


df[['zscore','label']] = df.groupby(['GROUP'])['VALUE'].apply(z_score)

print (df)

   GROUP  VALUE    zscore  label

0      1      5  1.135550      0

1      2      2  1.000000      0

2      1     10  1.297771      1

3      2     20  1.000000      1

4      1      7  0.162221      1

但是為了獲得更好的性能,可以在 out of 之后更改groupbyfor scoreonly 和labelcolumn count 的代碼groupby:


def z_score(x):

   z = np.abs(stats.zscore(x))

   return z


df['zscore'] = df.groupby('GROUP')['VALUE'].transform(z_score)

#lambda function alternative

#df['zscore'] = df.groupby('GROUP')['VALUE'].transform(lambda x: np.abs(stats.zscore(x)))

df['label'] = np.where(df['VALUE'] > 5, 1, 0)

print (df)

   GROUP  VALUE    zscore  label

0      1      5  1.135550      0

1      2      2  1.000000      0

2      1     10  1.297771      1

3      2     20  1.000000      1

4      1      7  0.162221      1


查看完整回答
反對 回復(fù) 2021-11-30
  • 1 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號