最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python else statement causes a continuous loop - Stack Overflow

programmeradmin3浏览0评论

Not understanding why my Python else statement is causing an infinite loop?

m = 0
while m < len(dataframe):
    if dataframe['firstname'].iloc[m] == 'Donald':
       dataframe.loc[m, 'lastname'] = 'Trump'
    elif dataframe['firstname'].iloc[m] == 'Joe':
       dataframe.loc[m, 'lastname'] = 'Biden'
    elif dataframe['firstname'].iloc[m] == 'Barrack':
       dataframe.loc[m, 'lastname'] = 'Obama'
    else:
       dataframe.loc[m, 'lastname'] = 'last name does not matter'
   
    m+=1

Not understanding why my Python else statement is causing an infinite loop?

m = 0
while m < len(dataframe):
    if dataframe['firstname'].iloc[m] == 'Donald':
       dataframe.loc[m, 'lastname'] = 'Trump'
    elif dataframe['firstname'].iloc[m] == 'Joe':
       dataframe.loc[m, 'lastname'] = 'Biden'
    elif dataframe['firstname'].iloc[m] == 'Barrack':
       dataframe.loc[m, 'lastname'] = 'Obama'
    else:
       dataframe.loc[m, 'lastname'] = 'last name does not matter'
   
    m+=1
Share Improve this question asked Jan 17 at 20:56 MrMeMrMe 435 bronze badges 3
  • 1 I can't reproduce this. Please provide a minimal reproducible example. – Barmar Commented Jan 17 at 21:09
  • I started with dataframe = pd.DataFrame({"firstname": ["Donald", "Joe", "Barrack", "Kamala"], "lastname": ["", "", "" ,""]}) – Barmar Commented Jan 17 at 21:10
  • If you had m+=1 at the beginning of the loop instead of the end, you would get an infinite loop because it assigns to the next row. When it reaches the last row of the df it adds a new row, so it never ends. – Barmar Commented Jan 17 at 21:12
Add a comment  | 

1 Answer 1

Reset to default 2

I have tested your code locally. I don't have infinite loop. Here is the code I have:

import pandas as pd
dataframe = pd.DataFrame({"firstname": ["Donald", "Joe", "Barrack", "Kamala"], "lastname": ["", "", "" ,""]})
m = 0
while m < len(dataframe):
    if dataframe['firstname'].iloc[m] == 'Donald':
       dataframe.loc[m, 'lastname'] = 'Trump'
    elif dataframe['firstname'].iloc[m] == 'Joe':
       dataframe.loc[m, 'lastname'] = 'Biden'
    elif dataframe['firstname'].iloc[m] == 'Barrack':
       dataframe.loc[m, 'lastname'] = 'Obama'
    else:
       dataframe.loc[m, 'lastname'] = 'last name does not matter'
   
    m+=1
print(dataframe)

I would recommend using for loops so you can't get infinite loops though:

import pandas as pd
dataframe = pd.DataFrame({"firstname": ["Donald", "Joe", "Barrack", "Kamala"], "lastname": ["", "", "" ,""]})
for i in range(len(dataframe)):
    if dataframe['firstname'].iloc[i] == 'Donald':
       dataframe.loc[i, 'lastname'] = 'Trump'
    elif dataframe['firstname'].iloc[i] == 'Joe':
       dataframe.loc[i, 'lastname'] = 'Biden'
    elif dataframe['firstname'].iloc[i] == 'Barrack':
       dataframe.loc[i, 'lastname'] = 'Obama'
    else:
       dataframe.loc[i, 'lastname'] = 'last name does not matter'
   
print(dataframe)

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论