我试图避免循环数据帧,因此最近开始使用 .apply() 。
但是我不太理解这种行为。下面我有一个超级简单的玩具示例。系统会询问用户该列中的每个水果是否都是苹果(它们都是苹果,因此每个答案都是 Y)。
import pandas as pd
df= pd.DataFrame({'fruit':['apple','apple', 'apple','apple', 'apple'],'result':['']*5})
df
fruit result
0 apple
1 apple
2 apple
3 apple
4 apple
设立一个.apply()
询问用户水果是否是苹果的函数:
def check_fruit(row):
# get the current fruit in the row
current_fruit = row['fruit']
# print output for user
print('\n===============================================')
print('Is this an apple?')
print('===============================================\n')
print(f'Current Fruit: {current_fruit}\n')
# user input - they are asked if the displayed fruit
# is an apple or not and must enter y/n
choice = input('Please enter Y/N: ')
# if they choose yes
if (choice == 'Y' or choice == 'y'):
# add the word 'correct' to row column
row['result']=='Correct'
return row
# if they choose no
elif (choice == 'N' or choice == 'n'):
# add the word 'Incorrect' to row column
row['result']=='Incorrect'
return row
现在应用它 - 注意输出。当数据框中只有 5 行时,为什么 apple 打印了 6 次?
df= df.apply(check_fruit,axis=1)
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
===============================================
Is this an apple?
===============================================
Current Fruit: apple
Please enter Y/N: y
其次,为什么应用函数没有返回该行?应用该函数后,“结果”列仍然为空。
fruit result
0 apple
1 apple
2 apple
3 apple
4 apple
我知道这可能是一件非常明显的事情......
知道我哪里出错了吗?
(ps.我知道输入没有错误检查,现在只关注 .apply() )