info
使用.info
方法,可以查看數據集的基本信息:
movies_df.info()
輸出
<class 'pandas.core.frame.DataFrame'> Index: 1000 entries, Guardians of the Galaxy to Nine Lives Data columns (total 11 columns): Rank 1000 non-null int64 Genre 1000 non-null object Description 1000 non-null object Director 1000 non-null object Actors 1000 non-null object Year 1000 non-null int64 Runtime (Minutes) 1000 non-null int64 Rating 1000 non-null float64 Votes 1000 non-null int64 Revenue (Millions) 872 non-null float64 Metascore 936 non-null float64 dtypes: float64(3), int64(4), object(4) memory usage: 93.8+ KB
上面的輸出信息中,包含了行和列的數量、非空值的數量、每個列中的數據類型以及DataFrame數據使用了多少內存。
可以看出,在Revenue
和Metascore
列中有一些缺失值,后面章節將會討論怎么處理這些缺失值。
快速查看數據類型很有用。例如,假設你剛剛導入了一些JSON,有些整數字段類型有可能被變為字符串,當計算時用到這些字段就會報“不支持的操作數”的錯。調用.info()
查看一下,就可以清楚看到整數列實際上都被變為了字符串。
shape
另一個有用的屬性是.shape
,表示DataFrame的形狀(行、列)。
movies_df.shape
輸出
(1000, 11)
注意,.shape
是屬性,不是函數(沒有圓括號),它是一個元組(行、列)。可以看到,數據集movies DataFrame中有1000行和11列。
在清理和轉換數據時,你可能會根據某些條件過濾一些行,然后想要知道刪除了多少行,就可以使用.shape
方法快速查看。