igneas
igneas

Reputation: 13

How do I make my code run in a for loop in python

I wanted to make this kinda code run in for loop rather me putting 3,6,9 and so on and similarly for total cars i.e 2,3,4. Because what I'm doing does not seem to be a good approach.

Code:

 data[(( data.total_people > 3 )& (data.total_cars < 2 )) | (( data.total_people > 6 )& (data.total_cars < 3 )) ]

Data:

 total_people   total_cars
2   4.0         1
7   5.0         1
19  4.0         1
21  6.0         1
40  4.0         1
43  4.0         1
48  4.0         1

Data is about people are more than 3 and car is only one. people are more than 6 and car is only 2 and so on for 9,12. I thought about adding 3 to total people like 3,6,9,12 and 1 each time to cars but how i'll do this in for loop.

Upvotes: 0

Views: 85

Answers (1)

Popescu David Ioan
Popescu David Ioan

Reputation: 106

I think you should clarify a bit. Is this what you had in mind?

for i in range(0, n):
    data[(( data.total_people > 3*(i+1) )& (data.total_cars == i+1 ))]    
    #process your data here

Where n is how many times you want to check

n = 1 => 3 people, 1 car

n = 2 => 6 people, 2 cars

n = 3 => 9 people, 3 cars etc

Upvotes: 1

Related Questions