1

I am new to Python and I was trying to create a loop which would create two lists of people based on the condition. My Dataframe has three columns:

controln donated amount_donated

controln is the ID of a person, donated means whether a person donated money or not

I'd like to have a list of people who donated more than $1 and another one which checks if they donated more than $5. I tried making a loop :

above5=[]
above1=[]

for a,c in val1.iterrows():
    a= val1['controln']
    b = val1['donated']
    c= val1['amount_donated']
    for item in c:
        if item >= 5 : 
          above5.append(a)
        if 5> item >= 1 :
            above1.append(a)
        else:
            print('No match')
print('Done') 

However it does not work as it creates a list of series with all the IDs and not those meeting the conditions. I tried also with above1.append((a,c)) but that also doesn't work.

Could anyone advise me on what should be changed?

atline
  • 23,297
  • 15
  • 58
  • 86
Antie
  • 11
  • 2

3 Answers3

1

You are interpreting a and c given by iterrows() incorrectly. The a gives you the index numbers while the c gives you the row. You can get the id from c[0], the donate boolean from c[1], and the amount donated from c[2]. Then you can use these and filter them out into appropriate lists using conditional statements. Also in python if 5> item >= 1 is an invalid conditional syntax.

You are also assigning the whole columns to a, b and c from the dataframe inside the loop by using statements such as a= val1['controln']. This is incorrect for your use case and also you should not reassign the a that you get from iterrows().

You also don't require a nested for loop for item in c: since it does not serve any purpose for your use case.

A correct sample code is as follows:

above5=[]
above1=[]

for a,b in df.iterrows():
    id = b[0]
    donate = b[1]
    val = b[2]
    
    if val >= 5: 
     above5.append(id)
    if val >= 1 and val < 5:
     above1.append(id)
    else:
     print('No match')
print('Done') 
CompEng007
  • 76
  • 8
0

try this:

above5=[]
above1=[]

for a,c in val1.iterrows():
    a = c['controln']
    b = c['donated']
    c = c['amount_donated']
    for item in c:
        if item >= 5 : 
            above5.append(a)
        if 5> item >= 1 :
            above1.append(a)
        else:
            print('No match')
print('Done') 
I'mahdi
  • 11,310
  • 3
  • 17
  • 23
  • Since c is overwritten to the donated amount, this should not work, since it is no longer a list. – Kartoffelkultur Sep 07 '21 at 10:50
  • @Antie welcome if this correct please read this link:https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – I'mahdi Sep 07 '21 at 10:50
  • @Kartoffelkultur c is overwriting but in the end of for c is get data from pandas in the for get data from list and .... – I'mahdi Sep 07 '21 at 10:52
  • but in your code you try a for loop with in int. That raises a `'numpy.int64' object is not iterable` error. I tried it. The code as you posted should and in my IDE do not work – Kartoffelkultur Sep 07 '21 at 10:55
  • @Kartoffelkultur you are wrong again type of c['amount_donated'] is `list` not `int`. when we have `for item in c:` we can understand that `c` is `list` not `int` – I'mahdi Sep 07 '21 at 10:56
  • But c is the row, `c['amount_donated']`is an int, an element of the list. It is the same with a. Or is a also a list ? Try your code with `val1 = pd.DataFrame({'amount_donated': [1, 2, 3], 'controln': [1, 2, 3], 'donated':[1, 2, 3]})` and it will fail – Kartoffelkultur Sep 07 '21 at 11:01
  • @Kartoffelkultur try this and don't get error : `val1 = pd.DataFrame({'amount_donated': [[1, 2, 3],[1, 2, 3],[1, 2, 3]], 'controln': [1, 2, 3], 'donated':[1, 2, 3]})` – I'mahdi Sep 07 '21 at 11:02
  • Yeah When you make `amount_donated`a list everything works. But the name amount_donated sounds more like an int to me – Kartoffelkultur Sep 07 '21 at 11:06
  • @Kartoffelkultur why are you argue with me? am I changing OP's code and question? see original code in the question, OP use `for item in c:` and we should understand `c` is `list` not `int`, OK?? – I'mahdi Sep 07 '21 at 11:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236844/discussion-between-kartoffelkultur-and-user1740577). – Kartoffelkultur Sep 07 '21 at 11:11
0

iterrows() return the row id and the row see a simple example .

So you should do something like this:

for a,c in val1.iterrows():
    amount = c['amount_donated']
    if amount >= 5 : 
        above5.append(c['controln'])
    elif amount >= 1 :
        above1.append(c['controln'])
    else:
        print('No match')
print('Done') 

When you write: a = val1['controln'] and appending a to the list, you actually appending the entire column.

Kartoffelkultur
  • 180
  • 1
  • 11