HOWTO · Pandas

Pandas DataFrame 尺寸

本教程解釋了 python pandas 屬性(例如 df.size、df.shape 和 df.ndim)的工作原理。

Python Pandas 庫附帶了一組可幫助我們執行各種任務的屬性。在使用 Pandas DataFrame 時,我們可能需要顯示 DataFrame 的大小、形狀和維度,我們可以使用一些流行的 Pandas 屬性(例如 df.sizedf.shape)輕鬆完成這項任務df.ndim

本文將演示如何使用 python pandas 屬性(如 dataframe.sizedataframe.shapedataframe.ndim] 返回或計算 DataFramesizeshapedimensions

使用 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 以 DataFrameseries_(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 屬性返回 seriesDataFrame 的維度。對於所有型別的 dataframesseries,它將返回維度 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.sizedataframe.shapedataframe.ndim 通過它們我們可以輕鬆返回 DataFrameseries 的大小、形狀和維度。希望以上所有的演示能夠幫助你理解 pandas 屬性的基本使用。