I have a loop where I constantly got an error message.
JavaScript
x
10
10
1
print(((df1['col1_df'] == 0) & (df1['col2_df'] == True)).sum())
2
print(((df1['col1_df'] == 0) & (df1['col3_df'] == True)).sum())
3
print(((df1['col1_df'] == 0) & (df1['col4_df'] == True)).sum())
4
print(((df1['col1_df'] == 0) & (df1['col5_df'] == True)).sum())
5
6
print(((df2['col1_df'] == 0) & (df2['col2_df'] == True)).sum())
7
print(((df2['col1_df'] == 0) & (df2['col3_df'] == True)).sum())
8
print(((df2['col1_df'] == 0) & (df2['col4_df'] == True)).sum())
9
print(((df2['col1_df'] == 0) & (df2['col5_df'] == True)).sum())
10
I want to loop them through a function.
So far I have:
JavaScript
1
4
1
for i in range (2,5):
2
col = "col{}_df".format(i)
3
print(((df['col'] == 0) & (df['col'] == 2)).sum())
4
How can I number the df and let the df go through 1, 2, 3, 4 (like df1, df2 df3)
Advertisement
Answer
col
is a variable. 'col'
is a string. Having df['col']
doesn’t refer to the variable col
.
Your string format is done wrong: col = "col{}_df".format(i)
Also, range(2,5)
will give you [2,5)
not [2,5]
. It is not inclusive.