📌  相关文章
📜  Python Pandas 中的数据框属性

📅  最后修改于: 2022-05-13 01:55:26.131000             🧑  作者: Mango

Python Pandas 中的数据框属性

在本文中,我们将讨论数据框的不同属性。属性是 DataFrame 的属性,可用于获取数据或与特定数据帧相关的任何信息。

编写属性的语法是:

这些是数据框的属性

  • 指数
  • 数据类型
  • 尺寸
  • 形状
  • ndim
  • 空的
  • 价值观

指数

DataFrame中有两种索引,一种是行索引,另一种是列索引。 index 属性用于显示数据框对象的行标签。行标签可以是 0,1,2,3,... 形式并且可以是名称。

示例 1:当 DataFrame 中未提及索引时

Python3
# Python program to implement
# index attribute in a dataframe object
import pandas as pd
 
# creating a 2D dictionary
dict = {"Student": ["Arnav", "Neha",
                    "Priya", "Rahul"],
        "Marks": [85, 92, 78, 83],
        "Sports": ["Cricket", "Volleyball",
                   "Hockey", "Badminton"]}
 
# creating a DataFrame
df = pd.DataFrame(dict)
 
# printing this DataFrame on the
# output screen
display(df)
 
# Implementing index attribute on
# this DataFrame
print(df.index)


Python3
# Python program to implement
# index attribute in a dataframe object
import pandas as pd
 
# creating a 2D dictionary
dict = {"Student": ["Arnav", "Neha",
                    "Priya", "Rahul"],
        "Marks": [85, 92, 78, 83],
        "Sports": ["Cricket", "Volleyball",
                   "Hockey", "Badminton"]}
 
# creating a DataFrame
df = pd.DataFrame(dict, index=['I', 'II', 'III', 'IV'])
 
# printing this DataFrame on the
# output screen
display(df)
 
# Implementing index attribute on
# this DataFrame
print(df.index)


Python3
# Python program to implement
# columns attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing index attribute for this
# data frame
print(data_frame.columns)


Python3
# Python program to implement
# axes attribute in a dataframe object
import pandas as pd
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha', 'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing axes attribute for this data frame
print(data_frame.axes)


Python3
# Python program to implement
# dtypes attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing dtypes attribute for this
# data frame
print(data_frame.dtypes)
 
# Now we will create another dataframe of same
# data type in a particular column
print("..Another data frame..")
 
# Creating a 2D dictionary
dict2 = {"Student": ["Arnav", "Neha",
                     "Priya", "Rahul"],
         "Marks": [85, 92, 78, 83],
         "Sports": ["Cricket", "Volleyball",
                    "Hockey", "Badminton"]}
 
# Creating another data frame object
data_frame = pd.DataFrame(dict2)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing dtypes attribute for this
# data frame
print(data_frame.dtypes)


Python3
# Python program to implement
# size attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing size attribute for this data frame
print("The total number of elements are:")
print(data_frame.size)


Python3
# Python program to implement
# shape attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing shape attribute for this data frame
print("Shape of the DataFrame:")
print(data_frame.shape)


Python3
# Python program to implement
# ndim attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam', 'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha', 'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing ndim attribute for this data frame
print("Number of Dimensions:")
print(data_frame.ndim)


Python3
# Python program to implement
# empty attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing empty attribute for this data frame
print("Is this DataFrame empty?")
print(data_frame.empty)
 
# Now we will create another dataframe
print("..Another data frame..")
 
# Creating a 2D empty dictionary
dict2 = {}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict2)
 
# printing this DataFrame on output screen
display(data_frame)
 
# Implementing empty attribute for this data frame
print("Is this DataFrame empty?")
print(data_frame.empty)


Python3
# Python program to implement T
# attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing T attribute for this data frame
print("Transpose of this DataFrame is:")
print(data_frame.T)


Python3
# Python program to implement values
# attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing values attribute for this data frame
print("NumPy Array form of this DataFrame is:")
print(data_frame.values)


输出:

在这个程序中,我们从 2D 字典创建了一个 DataFrame,然后在输出屏幕上打印了这个 DataFrame,在程序结束时,我们实现了一个索引属性(df.index)来打印这个 DataFrame 的索引标签。由于我们在这个程序中没有提到任何索引标签,它会自动将索引从 0 到 n 个数字,其中 n 是行数,然后打印在输出屏幕上。

示例 2:当 DataFrame 中提到索引时

Python3

# Python program to implement
# index attribute in a dataframe object
import pandas as pd
 
# creating a 2D dictionary
dict = {"Student": ["Arnav", "Neha",
                    "Priya", "Rahul"],
        "Marks": [85, 92, 78, 83],
        "Sports": ["Cricket", "Volleyball",
                   "Hockey", "Badminton"]}
 
# creating a DataFrame
df = pd.DataFrame(dict, index=['I', 'II', 'III', 'IV'])
 
# printing this DataFrame on the
# output screen
display(df)
 
# Implementing index attribute on
# this DataFrame
print(df.index)

输出:

在这个程序中,我们从 2D 字典中创建了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame,在程序结束时,我们实现了 index 属性(df.index)来打印这个 DataFrame 的索引标签,如我们在这个程序中提到了索引标签为 I、II、III 和 IV,因此它会在输出屏幕上打印相同的内容。

此属性用于获取特定数据框中存在的列的标签值。

Python3

# Python program to implement
# columns attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing index attribute for this
# data frame
print(data_frame.columns)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典中创建了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame,在程序结束时,我们将列属性实现为 print(data_frame.columns) 来打印此 DataFrame 的列标签。在这个程序中,列标签是“营销和销售”,所以它会打印相同的。

当我们想要一次获取所有行标签和所有列标签的值时使用此属性。

Python3

# Python program to implement
# axes attribute in a dataframe object
import pandas as pd
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha', 'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing axes attribute for this data frame
print(data_frame.axes)

输出:

在这个程序中,我们从 2D 字典中创建了一个 DataFrame,并将值作为字典对象,然后在输出屏幕上打印这个 DataFrame 在程序结束时,我们将轴属性实现为 print(data_frame.axes) 来打印此 DataFrame 的列标签和行标签。

数据类型

此属性的目的是显示特定数据框的每一列的数据类型。

Python3

# Python program to implement
# dtypes attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing dtypes attribute for this
# data frame
print(data_frame.dtypes)
 
# Now we will create another dataframe of same
# data type in a particular column
print("..Another data frame..")
 
# Creating a 2D dictionary
dict2 = {"Student": ["Arnav", "Neha",
                     "Priya", "Rahul"],
         "Marks": [85, 92, 78, 83],
         "Sports": ["Cricket", "Volleyball",
                    "Hockey", "Badminton"]}
 
# Creating another data frame object
data_frame = pd.DataFrame(dict2)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing dtypes attribute for this
# data frame
print(data_frame.dtypes)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典中制作了两个 DataFrame,然后将这些 DataFrame 打印在输出屏幕上。在每个 DataFrame 的末尾,我们实现了“dtypes”属性作为 print(data_frame.dtypes) 来打印两个 DataFrame 的每一列的数据类型。

尺寸

此属性用于显示数据框中存在的元素或项目的总数。

Python3

# Python program to implement
# size attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing size attribute for this data frame
print("The total number of elements are:")
print(data_frame.size)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典中创建了一个 DataFrame,然后将这个 DataFrame 打印在输出屏幕上。在程序的最后,我们实现了 size 属性为 print(data_frame.size) 来打印这个 DataFrame 的元素或项的总数。在这个数据框中,共有 6 个元素,其中 3 个元素来自第一列,3 个来自第二列。

形状

此属性用于显示特定数据框的总行数和列数。例如,如果我们在 DataFrame 中有 3 行 2 列,那么形状将是 (3,2)。

Python3

# Python program to implement
# shape attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23, 'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22, 'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing shape attribute for this data frame
print("Shape of the DataFrame:")
print(data_frame.shape)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典中创建了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame 在程序结束时,我们将 shape 属性实现为 print(data_frame.shape) 以打印数字此 DataFrame 的行和列。在他的 DataFrame 中,有 3 行 2 列,所以它会打印 (3,2)。

ndim

ndim 表示维数,该属性用于显示特定数据框的维数,DataFrame 是 2 维对象。

Python3

# Python program to implement
# ndim attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam', 'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha', 'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing ndim attribute for this data frame
print("Number of Dimensions:")
print(data_frame.ndim)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典创建了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame 在程序结束时,我们实现了 ndim 属性作为 print(data_frame.ndim) 来打印数字此 DataFrame 的维度。我们知道 DataFrame 是一个二维对象,所以它会打印 2。

空的

该属性用于检查数据框是否为空。如果数据框为空,则此属性返回 true,如果 DataFrame 不为空,则返回 false。

Python3

# Python program to implement
# empty attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing empty attribute for this data frame
print("Is this DataFrame empty?")
print(data_frame.empty)
 
# Now we will create another dataframe
print("..Another data frame..")
 
# Creating a 2D empty dictionary
dict2 = {}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict2)
 
# printing this DataFrame on output screen
display(data_frame)
 
# Implementing empty attribute for this data frame
print("Is this DataFrame empty?")
print(data_frame.empty)

输出:

在这个程序中,我们从 2D 字典中创建了两个 DataFrame,其值作为字典对象,然后在输出屏幕上打印这些 DataFrame 在每个 DataFrame 的末尾,我们实现了一个“空”属性作为 print(data_frame.empty) 到检查任何 DataFrame 是否为空。在这个程序中,第一个 DataFrame 不是空的,所以它会打印“False”,第二个 DataFrame 是空的,所以它会打印“True”。

T(转置)

此属性用于将行更改为列,将列更改为行。

Python3

# Python program to implement T
# attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing T attribute for this data frame
print("Transpose of this DataFrame is:")
print(data_frame.T)

输出:

在这个程序中,我们从一个具有值的二维字典作为字典对象制作了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame 在程序结束时,我们实现了“T”属性作为 print(data_frame.T) 来打印此 DataFrame 的转置。转置意味着 DataFrame 的所有行都将更改为列,反之亦然。

价值观

此属性用于以 NumPy 数组形式表示数据框的值/数据。

Python3

# Python program to implement values
# attribute in a dataframe object
import pandas as pd
 
# Creating a 2D dictionary having values as
# dictionary object
dict = {"Sales": {'Name': 'Shyam',
                  'Age': 23,
                  'Gender': 'Male'},
        "Marketing": {'Name': 'Neha',
                      'Age': 22,
                      'Gender': 'Female'}}
 
# Creating a data frame object
data_frame = pd.DataFrame(dict)
 
# printing this data frame on output screen
display(data_frame)
 
# Implementing values attribute for this data frame
print("NumPy Array form of this DataFrame is:")
print(data_frame.values)

输出:

在这个程序中,我们从具有值作为字典对象的 2D 字典创建了一个 DataFrame,然后在输出屏幕上打印这个 DataFrame 在程序结束时,我们实现了“values”属性作为 print(data_frame.values)以 NumPy 数组的形式打印这个 DataFrame 的所有数据。