Q:

Error in Iterating through Pandas DataFrame with if statement

I have the following pandas DataFrame.
   Id UserId    Name            Date                 Class  TagBased
0   2   23  Autobiographer  2016-01-12T18:44:49.267     3   False
1   3   22  Autobiographer  2016-01-12T18:44:49.267     3   False
2   4   21  Autobiographer  2016-01-12T18:44:49.267     3   False
3   5   20  Autobiographer  2016-01-12T18:44:49.267     3   False
4   6   19  Autobiographer  2016-01-12T18:44:49.267     3   False

I want to iterate through "TagBased" column and put the User Ids in a list where TagBased=True.
I have used the following code but I am getting no output which is incorrect because there are 18 True values in TagBased.
user_tagBased = []

for i in range(len(df)):
    if (df['TagBased'] is True):
        user_TagBased.append(df['UserId'])
print(user_TagBased)

Output: []

A:

As others are suggesting, using Pandas conditional filtering is the best choice here without using loops! However, to still explain why your code did not work as expected:
You are appending df['UserId'] in a for-loop while df['UserId'] is a column. Same goes for df['TagBased'] check, which is also a column.
I assume you want to append the userId at the current row in the for-loop.
You can do that by iterating through the df rows:
user_tagBased = []

for index, row in df.iterrows():
    if row['TagBased'] == 'True': # Because it is a string and not a boolean here
        user_tagBased.append(row['UserId'])

