了解使用 pandas.DataFrame.apply 时的 None 输出

Understanding the None output when using pandas.DataFrame.apply

我正在尝试使用 pandas.DataFrame.apply 函数。我的实际代码执行类似于下面的示例。在输出的末尾,它为数据帧中的每一行输出 "None"。此行为会导致我通过 apply.

传递的函数出错

df = pd.DataFrame({"one": range(0,5), "two": range(0,5)})

df.apply(print, axis=1)

  1. 为什么会这样? None 的来源是什么?
  2. 我怎样才能 alter/control 这种行为?

None 发生是因为 print() 函数没有 return 任何值,并且 apply() 期望函数 return 某些东西。

如果您想打印数据框,只需使用 print(df),或者如果您需要其他格式,请告诉我们您想要在打印输出中获得什么。