codelearner0504
codelearner0504

Reputation: 71

Loop through a few commands using a function

I have a loop where I constantly got an error message.

print(((df1['col1_df'] == 0) & (df1['col2_df'] == True)).sum())
print(((df1['col1_df'] == 0) & (df1['col3_df'] == True)).sum())
print(((df1['col1_df'] == 0) & (df1['col4_df'] == True)).sum())
print(((df1['col1_df'] == 0) & (df1['col5_df'] == True)).sum())

print(((df2['col1_df'] == 0) & (df2['col2_df'] == True)).sum())
print(((df2['col1_df'] == 0) & (df2['col3_df'] == True)).sum())
print(((df2['col1_df'] == 0) & (df2['col4_df'] == True)).sum())
print(((df2['col1_df'] == 0) & (df2['col5_df'] == True)).sum())

I want to loop them through a function.

So far I have:

for i in range (2,5):
    col = "col{}_df".format(i)
    print(((df['col'] == 0) & (df['col'] == 2)).sum())

How can I number the df and let the df go through 1, 2, 3, 4 (like df1, df2 df3)

Upvotes: 1

Views: 39

Answers (2)

Amal K
Amal K

Reputation: 4899

You can express your entire code in two lines using a comprehension:

print(*( ((df1[f'col1_df'] == 0) & (df1[f'col{i}_df'] == True)).sum() for i in range(2,6) ), sep="\n")
print(*( ((df2[f'col1_df'] == 0) & (df2[f'col{i}_df'] == True)).sum() for i in range(2,6) ), sep="\n")

The expression ((df1[f'col1_df'] == 0) & (df1[f'col{i}_df'] == True)).sum() for i in range(2,6) ) creates a generator object yielding the 4 expressions one by one.

The * scatters the elements in this generator and passes the arguments to print as you would have passed a comma-separated argument list. The end=\n ensures that each of these arguments are separated by a new line when output.

Upvotes: 0

noah
noah

Reputation: 2776

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.

Upvotes: 1

Related Questions