首页 文章资讯内容详情

熊猫是否依赖于 NumPy?

2026-06-02 1 花语

Pandas建立在NumPy之上,这意味着Pythonpandas包依赖于NumPy包以及许多其他3rd方库的Pandas。所以我们可以说Numpy是操作Pandas所必需的。

pandas库在很大程度上依赖于Numpy数组来实现pandas数据对象。

示例

import pandas as pd df = pd.DataFrame({A:[1,2,3,4], B:[5,6,7,8]}) print(Type of DataFrame: ,type(df)) print(Type of single Column A: ,type(df[A])) print(Type of values in column A,type(df[A].values)) print(df[A].values)

解释

df变量存储了一个使用python字典创建的DataFrame对象,这个DataFrame有2列,分别命名为A和B。在上面代码的第三个中,我们试图显示我们的dataFrame的类型,它将显示Pandas核心Dataframe。第四行将打印单列的类型,即A结果输出将是熊猫系列。第五行将显示该单列A中可用的值类型。

输出结果

Type of DataFrame: <class pandas.core.frame.DataFrame> Type of single Column A: <class pandas.core.series.Series> Type of values in column A <class numpy.ndarray> array([1, 2, 3, 4], dtype=int64)

输出的第三行显示数据代表我们上面的熊猫示例中的Numpy数组对象。在我们的示例中,我们甚至没有导入NumPy包。

示例

import pandas as pd df = pd.DataFrame([[a,b],[c,d],[e,f],[g,h]], columns=[col1,col2]) print(Type of DataFrame: ,type(df)) print(Type of single Column A: ,type(df[col1])) print(Type of values in column A,type(df[col1].values)) print(df[col1].values)

解释

在下面的示例中,我们有一个使用python列表列表创建的DataFramedf。这个DataFramedf有2列名为col1和col2。我们尝试打印单列“col1”的类型,结果输出将是熊猫系列。如果我们打印该列col1中可用值的类型,我们可以看到输出将是numpy.ndarray。

输出结果

Type of DataFrame: <class pandas.core.frame.DataFrame> Type of single Column A: <class pandas.core.series.Series> Type of values in column A <class numpy.ndarray> [a c e g]

现在我们可以说pandas列可以建立在NumPy数组对象的基础上。在使用Pandas时,您不需要专门导入它。当你安装Pandas时,你可以看到如果你之前没有安装过NumPy,你的包管理器会自动安装Numpy包。