如何實現python裡面根據邏輯判斷新增新的列?

時間 2021-07-06 04:07:59

1樓:星空流

介紹三種方法,都是基於pandas。

原資料如下:

defjudge_edu

(edu

):if

edu<

3.0:

return

'lower'

elif

edu==

3.0:

return

'middle'

else

:return

'high'

data

['judage_edu']=

data

['education'].

(judge_edu

)執行結果如下:

2. 使用np.where()

data['judge_edu1'] = np.where(data.education<3.

0,'lower',np.where(data.education==3.

0,'middle','high'))

執行結果如下:

3. 使用np.select()

conditions = [data.education<3.0,data.education==3.0,data.education>3.0]

selects = ['lower','middle','high']

data['judge_edu2'] = np.select(conditions,selects)

執行結果如下:

三種方法都能得到想要的結果,不同之處在於執行速度,資料量大的時候特別明顯。

python 如何實現遠端tail f?

37丫37 django channels寫個也不複雜,這裡有關於channels的介紹,也有實現tailf的例子 Django使用Channels實現WebSocket 上篇 guyskk tail f是不會結束的,所以你要開新執行緒去讀取日誌並寫入django response 並且respon...

Python 如何實現月份相加?

奚衡 受邀答題,自己DIY了乙個計算這個的函式,思路大致 1 計算出這段指定時間的當天時間凌晨時間和疊加月後的凌晨時間 2 求出相差的天數 3 最後再加天數再指定的時間內疊加即可 usr bin env python coding utf 8 from datetime import datetim...

python如何實現列表交叉合併

蓋世小猛男 all list length1 len res list length2 len res list2 if length1 length2 length length2 elif length1 length2 length length1 else length length1 fo...