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

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

如何在Pandas中根據(jù)兩個現(xiàn)有列的條件填充新列?

如何在Pandas中根據(jù)兩個現(xiàn)有列的條件填充新列?

動漫人物 2022-09-20 17:55:40
我正在嘗試根據(jù)兩個現(xiàn)有列的條件創(chuàng)建一個新列,但是在使用“np.where”后出現(xiàn)錯誤,有沒有其他方法可以實(shí)現(xiàn)這一點(diǎn)?輸入:change1 change2yes     yesyes     nono      yesno      yes預(yù)期輸出:change1 change2 change3yes      yes      okyes      no       not okno       yes      not okno       yes      not ok法典:import pandas as pdimport numpy as npdf1=pd.read_csv('test2.txt',sep='\t')df1['change1'] = df1['change1'].astype(str)df1['change2'] = df1['change2'].astype(str)df['change3'] = np.where(df1['change1']=='yes' & df1['change2'] == 'yes', 'ok', 'not ok')print(df1)錯誤:cannot compare a dtyped [object] array with a scalar of type [bool]
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個贊

使用數(shù)據(jù)幀.eq 和 DataFrame.all.這將幫助您改進(jìn)代碼的語法并避免錯誤。

df['change3'] = np.where(df.eq('yes').all(axis=1), 'ok' , 'not ok')

#if you need select columns

#df['change3'] = np.where(df[['change1', 'change2']].eq('yes').all(axis=1),

                          'ok' , 'not ok')

沒有數(shù)據(jù)幀。

df['change3'] = np.where((df1['change1']=='yes') & (df1['change2'] == 'yes'), 
                         'ok', 'not ok')

df['change3'] = np.where(df1['change1'].eq('yes') & df1['change2'].eq('yes'), 
                         'ok', 'not ok')

您也可以使用系列地圖 / 系列替換

df['change3'] = df.eq('yes').all(axis=1).map({True : 'ok' , False : 'not ok'})
#df['change3'] = df.eq('yes').all(axis=1).replace({True : 'ok' , False : 'not ok'})

print(df)


#   change1 change2 change3

# 0     yes     yes      ok

# 1     yes      no  not ok

# 2      no     yes  not ok

# 3      no     yes  not ok


查看完整回答
反對 回復(fù) 2022-09-20
?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個贊

用于轉(zhuǎn)換為二進(jìn)制,然后檢查每行:DataFrame.replaceall


df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).all(axis=1), 

                          'ok', 

                          'not ok')

或與 和 :replacesum


df1['change3'] = np.where(df1.replace({'yes': 1, 'no': 0}).sum(axis=1).gt(1), 

                          'ok', 

                          'not ok')

  change1 change2 change3

0     yes     yes      ok

1     yes      no  not ok

2      no     yes  not ok

3      no     yes  not ok


查看完整回答
反對 回復(fù) 2022-09-20
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個贊

您可以使用:

df['change3'] = df.apply(lambda x: 'ok' if x['change1'] == x['change2'] else 'not ok', axis=1)

輸出:

查看完整回答
反對 回復(fù) 2022-09-20
  • 3 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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