简体   繁体   English

使用 DataFrame.apply 时,如何使 Python 函数仅返回两个值之一?

[英]How can I make a Python function return only one of two values when using DataFrame.apply?

If I use this fuction line1.apply(sc.shapiro, axis=1) .如果我使用这个函数line1.apply(sc.shapiro, axis=1) it gives me the result like this (0.9815108776092529, 0.9715939164161682) .它给了我这样的结果(0.9815108776092529, 0.9715939164161682) I want to get only the second value, so I tried to write it like this line1.apply(sc.shapiro[1], axis=1) but it never worked :(我只想得到第二个值,所以我试着像这样写line1.apply(sc.shapiro[1], axis=1)但它从来没有工作:(

The problem is问题是

----> 1 line1.apply(sc.shapiro[1], axis=1) 

TypeError: 'function' object is not subscriptable

Any suggestions?有什么建议?

You need to slice out the [1] from the actual result, not the method argument: line1.apply(sc.shapiro, axis=1)[1] .您需要从实际结果中切出[1] ,而不是方法参数: line1.apply(sc.shapiro, axis=1)[1] And since your input argument is a function, that also explains your error message.由于您的输入参数是一个函数,这也解释了您的错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DataFrame.apply()生成值时返回Synset('')包装器 - Returning Synset('') wrapper when using DataFrame.apply() to generate values Python / pandas - 使用DataFrame.apply和函数返回字典 - Python/pandas - Using DataFrame.apply with function returning dictionary 在python中使用DataFrame.apply时如何访问以前的值? - How to access the previous value while using DataFrame.apply in python? 如何将参数动态传递给DataFrame.apply()lambda函数? - How can I pass arguments dynamically to a DataFrame.apply() lambda function? python pandas DataFrame.apply:返回系列而不是数据框 - python pandas DataFrame.apply: return a series instead of a dataframe 当返回dict时,DataFrame.apply(func)不起作用 - DataFrame.apply(func) doesn't work when return is dict 为什么和何时在dataframe.apply中给出的结果不同于在单个元素上使用函数的结果? - Why and when does dataframe.apply give different results than using the function on an individual element? Python:如何传递行和下一行 DataFrame.apply() 方法? - Python: How to pass row and next row DataFrame.apply() method? 如何使用应用程序从 python function 将多行返回到 pandas Z6A8064B5DF479455500553C7? - How can I return multiple rows from a python function to a pandas dataframe using apply? 如何使用 DataFrame.apply() 将行内容作为字符串参数传递? - How to pass row content as string argument using DataFrame.apply()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM