首页 > 解决方案 > 尝试使用 numpy 在 Python 中创建自定义函数

问题描述

我正在尝试创建一个自定义函数来输出我的数据框的详细信息。我在我的 jupyter 笔记本上运行以下代码,但它只是运行而没有显示任何输出。

def details(mydf,n):
    mydf.shape
    mydf.dtypes
    mydf.head(n)
    mydf.tail(n)
    return None
mydf=pd.read_csv("House.Price.csv",low_memory=False)
details(mydf,5)

标签: pythonfunctionnumpy

解决方案


要显示jupyter使用中的数据功能display(<arg>),如下所示:

def details(mydf,n):
   display(mydf.shape)
   display(mydf.dtypes)
   display(mydf.head(n))
   display(mydf.tail(n))

编辑

display()函数还可以配置您要输出多少行或列。

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_rows', None) // for all
pd.set_option('display.max_columns', 500)

你正在做的是调用一个返回的函数,None因此没有输出,因为默认情况下jupyter只打印块的最后一行返回的值。


推荐阅读