为什么我的 .apply() 函数也输出一系列“无"? [英] Why is my .apply() function also outputting a series of 'None'?
本文介绍了为什么我的 .apply() 函数也输出一系列“无"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能不是什么大问题,我只是在执行 .apply() 之前没有注意到 None
的这个输出
This may not be a big problem, I just haven't noticed this output of None
before when doing .apply()
玩具示例:
mydf = pd.DataFrame({'col1':['test1',np.nan,'test3','test4'],
'col2':['test5','test6','test7','test8']})
mydf
col1 col2
0 test1 test5
1 NaN test6
2 test3 test7
3 test4 test8
将值加到一个字符串中的函数:
Function to just add the values together in a string:
def myfunc(row):
thing = str(row['col1']) + str(row['col2'])
print(thing)
应用:
mydf.apply(myfunc,axis=1)
我的输出:
test1test5
nantest6
test3test7
test4test8
0 None
1 None
2 None
3 None
dtype: object
有什么值得担心的吗?我将很快将这样的东西应用于一些真实数据.如果它有所作为,我将在 Jupyter Notebook 中执行此操作.
Is it something to be worried about? I will be applying something like this to some real data shortly. I am doing this in Jupyter Notebook if it makes a difference.
推荐答案
你应该返回字符串而不是打印它.
You should return the string instead of printing it.
这篇关于为什么我的 .apply() 函数也输出一系列“无"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文