I have two dataframe df1
and df2
,
df1
A B
2 6
5 1
7 3
1 2
9 7
4 7
3 4
8 9
and df2
contains
A B A_bin B_bin C D E
2 6 1 2 5 4 1
5 1 2 1 2 2 4
7 3 3 1 5 1 7
1 2 1 1 8 4 9
9 7 3 3 5 5 8
4 7 2 3 1 8 5
3 4 1 2 2 9 3
8 9 3 3 4 6 2
I am trying to select only those specific rows selected from df2
to a new data frame df_result_A
for all the row that has A_bin = 1
similarily, a separate data frame df_result_B
having all those rows of df2
such that B_bin
rows contain 1.
I am finding it difficult to put my logic incorrect syntax or probably my logic is wrong,
for i in range(len(df1(df2[columns])+len(df)):
if(row value is 1)
print in df_result_A
print in df_result_B
As the challenge is to not use column name and indexing, as the code should run for other data set as well I am trying to first iterate over the first two column of df2 as len(df1)
will let me know that after 2 columns A_bin
and B_bin
will come.
thus, when I am on the first column of df2 then adding len(df1)
will put me on A_bin
and iterating over it for checking value to be 1 and storing it in a separate dataframe.
Similarly, when I am on 2nd column of df2
adding len(df2)
will put me on B_bin
and thus storing its result in df_result_B
.
expected result in separate dataframe.
df_result_A
A B C D E
2 6 5 4 1
1 2 8 4 9
3 4 2 9 3
df_result_b
A B C D E
5 1 2 2 4
7 3 5 1 7
1 2 8 4 9