pandas 按条件新增列
import pandas as pd
import numpy as np
df=pd.DataFrame({'amount':[100,200,300,400,500],'list':['','商品1','商品2','','商品3']})
df['x1'] = df.apply(lambda x: x.amount if x.list != "" else 0, axis=1)
df['x2']=np.where(df['list']=='',0,df['amount'])
df.loc[df['list']!='','x3']=df['amount']
df=df.fillna(0)
评论
发表评论