英文:
Adding (and) logic for a list of columns and generating feature
问题
I have translated the relevant code section for you:
df2['alpha_flag'] = np.where((df2['A'] == 1) & (df2['B'] == 1) & (df2['D'] == 1), 1, 0)
This code calculates the 'alpha_flag' based on the conditions specified in your description.
英文:
I have requirement to create a feature by checking the fields of two different dataframes.
df1 is a dataframe created from mysqldb and it will be static, the values of column(required) from df1 can be moved from True or False.
below are more details.
df1=
flag beta required
alpha A TRUE
alpha B TRUE
alpha C FALSE
alpha D TRUE
df2=
name A B C D E F
roy 1 1 0 1 0 0
john 0 1 1 1 0 0
sam 1 1 1 1 1 1
I trying to create feature alpha_flag by using the column name "beta" from df1 if the required column is TRUE.
So the code for alpha_flag will be like below
df2['alpha_flag'] = np.where(df2['A']==1 and df2['B']==1 and df2['D']==1 , 1 , 0)
The difficulty i am facing is to choose only the columns that are mentioned as required true from df1 and dynamically update my 'alpha_flag' creation condition.
答案1
得分: 1
Sure, here is the translated code portion:
df2['alpha_flag'] = df2[df1.loc[df1['required'].eq('TRUE'), 'beta'].tolist()].eq(1).all(axis=1).astype(int)
如果所有'required'的值都是'FALSE',这将导致从'df2'中选择空列,并导致对空数据框进行操作,可能会导致意外行为,因此更健壮的版本如下:
cols = df1.loc[df1['required'], 'beta'].tolist()
df2['alpha_flag'] = df2[cols].eq(1).all(axis=1).astype(int) if len(cols) else 0
英文:
IIUC, you can use boolean indexing to select the beta
rows where required
is TRUE
, then check if these columns in df2
is all equal with 1
df2['alpha_flag'] = df2[df1.loc[df1['required'].eq('TRUE'), 'beta'].tolist()].eq(1).all(axis=1).astype(int)
Note that if all required
value is FALSE
, this will cause selecting empty column from df2
and lead to empty dataframe manipulation which might cause unexpected behavior, so a more robust version would be
cols = df1.loc[df1['required'], 'beta'].tolist()
df2['alpha_flag'] = df2[cols].eq(1).all(axis=1).astype(int) if len(cols) else 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论