Pandas DataFrame 尺寸
-
使用
dataframe.size属性在 Pandas Python 中显示DataFrame大小 -
使用
dataframe.shape属性在 Pandas Python 中显示DataFrame形状 -
使用
dataframe.ndim属性在 Pandas Python 中显示DataFrame维度 - 结论
Python Pandas 库附带了一组可帮助我们执行各种任务的属性。在使用 Pandas DataFrame 时,我们可能需要显示 DataFrame 的大小、形状和维度,我们可以使用一些流行的 Pandas 属性(例如 df.size、df.shape)轻松完成这项任务 和 df.ndim。
本文将演示如何使用 python pandas 属性(如 dataframe.size、dataframe.shape 和 dataframe.ndim] 返回或计算 DataFrame的size、shape和dimensions 。
使用 dataframe.size 属性在 Pandas Python 中显示 DataFrame 大小
在 python Pandas DataFrame 中。size 属性用于显示 Pandas DataFrame 的大小。它返回 DataFrame 或等于元素总数的系列的大小。如果要计算 series 的大小,它将返回行数。在 DataFrame 的情况下,它将返回行乘以列。
在以下示例中,我们使用 pd.csvread() 导入或读取 a .csv 文件并创建了一个 DataFrame。使用 Pandas 属性 dataframe.size,我们显示给定 DataFrame 的大小。
示例代码:
import pandas as pd
# create a dataframe after reading .csv file
dataframe = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\CSV_files\Samplefile1.csv")
# print dataframe
print(dataframe)
# displaying dataframe size
print("The size of the DataFrame is: ", dataframe.size)
输出:
Name Team Position Age
0 Adam Donachie "BAL" "Catcher" 22.99
1 Paul Bako "BAL" "Catcher" 34.69
2 Ramon Hernandez "BAL" "Catcher" 30.78
3 Kevin Millar "BAL" "First Baseman" 35.43
4 Chris Gomez "BAL" "First Baseman" 35.71
5 Brian Roberts "BAL" "Second Baseman" 29.39
6 Miguel Tejada "BAL" "Shortstop" 30.77
7 Melvin Mora "BAL" "Third Baseman" 35.07
8 Aubrey Huff "BAL" "Third Baseman" 30.19
9 Adam Stern "BAL" "Outfielder" 27.05
The size of the DataFrame is: 40
使用 dataframe.shape 属性在 Pandas Python 中显示 DataFrame 形状
dataframe.shape 属性 pandas python 以 DataFrame 或 series 的 _(rows, columns)_ 形式返回元组形状。
在我们下面提供的代码示例中,我们在读取 .csv 文件后创建了一个 DataFrame。要返回 dataframe.shape,我们按以下方式使用 dataframe.shape 属性:
示例代码:
import pandas as pd
# create a dataframe after reading .csv file
dataframe = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\CSV_files\Samplefile1.csv")
# print dataframe
print(dataframe)
# displaying dataframe shape
print("The shape of the DataFrame is: ", dataframe.shape)
输出:
Name Team Position Age
0 Adam Donachie "BAL" "Catcher" 22.99
1 Paul Bako "BAL" "Catcher" 34.69
2 Ramon Hernandez "BAL" "Catcher" 30.78
3 Kevin Millar "BAL" "First Baseman" 35.43
4 Chris Gomez "BAL" "First Baseman" 35.71
5 Brian Roberts "BAL" "Second Baseman" 29.39
6 Miguel Tejada "BAL" "Shortstop" 30.77
7 Melvin Mora "BAL" "Third Baseman" 35.07
8 Aubrey Huff "BAL" "Third Baseman" 30.19
9 Adam Stern "BAL" "Outfielder" 27.05
The shape of the DataFrame is: (10, 4)
使用 dataframe.ndim 属性在 Pandas Python 中显示 DataFrame 维度
Pandas dataframe.ndim 属性返回 series 或 DataFrame 的维度。对于所有类型的 dataframes 和 series,它将返回维度 1 的 _series_ 仅由行组成,并在 DataFrame 或二维数据的情况下返回 2。
在下面的示例代码中,我们通过导入 .csv 文件创建了一个 DataFrame。为了返回 DataFrame 的维度,我们使用了 dataframe.ndim pandas 属性,在 pandas DataFrame 的情况下是 2。
示例代码:
import pandas as pd
# create a dataframe after reading .csv file
dataframe = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\CSV_files\Samplefile1.csv")
# print dataframe
print(dataframe)
# displaying dataframe dimension
print("The dimension of the DataFrame is: ", dataframe.ndim)
输出:
Name Team Position Age
0 Adam Donachie "BAL" "Catcher" 22.99
1 Paul Bako "BAL" "Catcher" 34.69
2 Ramon Hernandez "BAL" "Catcher" 30.78
3 Kevin Millar "BAL" "First Baseman" 35.43
4 Chris Gomez "BAL" "First Baseman" 35.71
5 Brian Roberts "BAL" "Second Baseman" 29.39
6 Miguel Tejada "BAL" "Shortstop" 30.77
7 Melvin Mora "BAL" "Third Baseman" 35.07
8 Aubrey Huff "BAL" "Third Baseman" 30.19
9 Adam Stern "BAL" "Outfielder" 27.05
The dimension of the DataFrame is: 2
结论
我们探索了三种不同的 Pandas 属性,例如 dataframe.size、dataframe.shape 和 dataframe.ndim 通过它们我们可以轻松返回 DataFrame 或 series 的大小、形状和维度。希望以上所有的演示能够帮助你理解 pandas 属性的基本使用。