简体   繁体   中英

I struggle a lot to apply loop in dataframe, How can I improve this?

for loop in dataframe I tried this can anyone help me with the correct answer

for i in df['name']:
    if df['name'] = 'Amit':
        print('yes Amit')
    else:
        df['name'] = 'not Amit'
name age city
Amit 45 Pune
Ajay 25 Pune

I don't know how is your dataframe but this:

for i, row in df.iterrows():
    if row['name'] == 'Amit':
        df.loc[i, 'name'] = 'yes Amit'
    else:
        df.loc[i, 'name'] = 'not Amit'

Might do what you want.

If we assume that your dataframe is:

df_data = {
     'name' : ['Amit','Peter','Carl','Mary','Amit','Hank','Ali'],
     'age': [45,34,23,56,21,23,45]
     }
df = pd.DataFrame(df_data)
print(df)

Output:

    name  age
0   Amit   45
1  Peter   34
2   Carl   23
3   Mary   56
4   Amit   21
5   Hank   23
6    Ali   45

Basically, if you want to run a 'loop', you should do something like this:

for index, row in df.iterrows():
  if df['name'][index] == 'Amit':
    print('yes Amit')
  else:
    print('not Amit')

Output:

yes Amit
not Amit
not Amit
not Amit
yes Amit
not Amit
not Amit

But usually it is better NOT to do the "loop", and there are always more optimized methods!

df['name'].apply(lambda x: 'yes Amit' if x == 'Amit' else 'not Amit')

Output:

0    yes Amit
1    not Amit
2    not Amit
3    not Amit
4    yes Amit
5    not Amit
6    not Amit

And also:

df['Y/N'] = df['name'] == 'Amit'

Output:

    name  age    Y/N
0   Amit   45   True
1  Peter   34  False
2   Carl   23  False
3   Mary   56  False
4   Amit   21   True
5   Hank   23  False
6    Ali   45  False

Also take a look at: How to iterate over rows in a DataFrame in Pandas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM