gpt4 book ai didi

python - 试图理解 Pandas 中的 .apply()

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:55 24 4
gpt4 key购买 nike

我试图避免循环遍历数据帧,所以最近开始使用 .apply()。

但是我并不真正理解这种行为。我在下面有一个 super 简单的玩具示例。询问用户列中的每个水果是否都是苹果(它们都是苹果,所以每个答案都是 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

现在应用它 - 注意输出。为什么 apple 在数据框中只有 5 行时打印了 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())

最佳答案

请参阅 pd.DataFrame.apply 的文档:

Notes


In the current implementation apply calls func twice on the first column/row to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first column/row.

您的函数 check_fruit 确实有副作用,即要求用户进行一些输入,这种情况发生的次数超出您的预期。

通常,apply 和其他数据框函数旨在与以某种方式转换数据的函数一起使用,而不是与应用程序逻辑一起使用。在这种情况下,不显式写出循环不会给您带来任何特别的好处,因此您能做的最好的事情可能就是手动遍历每一行:

import pandas as pd

def check_fruit(row):
# ...

df = pd.DataFrame({'fruit': ['apple', 'apple', 'apple', 'apple', 'apple'],
'result': [''] * 5})
for row in df.iterrows():
check_fruit(row)

关于python - 试图理解 Pandas 中的 .apply(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57527661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com