pandas 条件赋值
df["WhereCol"] = np.where((df.Cat == "A") & (df.B > 10), 1, 0)
conditions = [
(df.Cat == "A") & (df.B > 10),
(df.Cat == "B") & (df.B > 10)
]values = [1, 2]df["SelectCol"] = np.select(conditions, values, default=0)xg.case_when(xg.A3.str.slice(16,17).astype(int) % 2 ==0,'女',
~xg.A3.str.slice(16,17).astype(int) % 2 ==0,'男',
'不详',
column_name="性别")
xg=xg.case_when(xg.A3.str.contains('^62'),'甘肃',
~xg.A3.str.contains('^62'),'外省',
'其他',
column_name="归属")
评论
发表评论