id. datcol1 datacol2 datacol-n final col(to be created in output)
1 false true true 0
2 false false false 2
3 true true true 0
4 true false false 1
有多個列說 13,所以作業是在所有列中獲取每個行 id,并檢查列是否至少或等于兩個“真”字串,然后分配 0 ;如果一個“真”字串則分配 1,如果根本沒有“真”則分配 2
uj5u.com熱心網友回復:
考慮 df 為:
In [1542]: df
Out[1542]:
id. datcol1 datacol2 datacol-n
0 1 False True True
1 2 False False False
2 3 True True True
3 4 True False False
使用numpy.select,和: df.filter_Series.gedf.sum
In [1546]: import numpy as np
In [1547]: x = df.filter(like='dat').sum(1)
In [1548]: conds = [x.ge(2), x.eq(1), x.eq(0)]
In [1549]: choices = [0, 1, 2]
In [1553]: df['flag'] = np.select(conds, choices)
In [1554]: df
Out[1554]:
id. datcol1 datacol2 datacol-n flag
0 1 False True True 0
1 2 False False False 2
2 3 True True True 0
3 4 True False False 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430673.html
標籤:Python python-3.x 熊猫 细绳 麻木的
上一篇:試圖在字串的索引中插入字符
