![](/img/trans.png)
[英]Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column
[英]How to create a new column based on specific considerations of other columns - Python
使用df["Buy_intention"] = np.where(df['Difference'] > df['Mean'], 1, 0)
代碼-
import pandas as pd
import numpy as np
data={'email':['xyz@xyz.com','xyz1@xyz.com','xyz2@xyz.com','xyz3@xyz.com'],'Difference':[15,23,45,23],'Mean':[14,26,42,29]}
df=pd.DataFrame(data)
df["Buy_intention"] = np.where(df['Difference'] > df['Mean'], 1, 0)
df
輸出-
email Difference Mean Buy_intention
0 xyz@xyz.com 15 14 1
1 xyz1@xyz.com 23 26 0
2 xyz2@xyz.com 45 42 1
3 xyz3@xyz.com 23 29 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.