DataFrame 基础检查
使用 head()、tail()、info()、describe() 和 shape 快速了解任意 DataFrame 的内容和结构。
DataFrame 基础检查 是 CoddyKit 上的免费 Pandas & NumPy Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Pandas & NumPy Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Pandas & NumPy Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The First Five Steps with Any DataFrame
Whenever you load a new dataset, a systematic inspection sequence saves hours of debugging. The Pandas toolkit for this includes: head() to see the first rows, tail() for the last rows, info() for column types and nulls, describe() for statistics, and shape for dimensions. Running these five checks takes less than a minute and reveals most data quality issues immediately.
import pandas as pd
# Assume df is loaded from a CSV
print(df.shape) # (rows, cols)
df.head() # first 5 rows
df.info() # dtypes + non-null counts
df.describe() # statisticshead() and tail()
df.head(n) returns the first n rows (default 5) as a DataFrame. df.tail(n) returns the last n rows. Together they help you verify that the data was parsed correctly — checking that column names are in the header row, that numeric columns contain numbers, and that date strings were not misinterpreted. They are non-destructive and return new DataFrames.
import pandas as pd
df = pd.DataFrame({'a': range(20), 'b': range(20, 40)})
print(df.head(3))
# a b
# 0 0 20
# 1 1 21
# 2 2 22
print(df.tail(2))
# a b
# 18 18 38
# 19 19 39info(): Types and Non-Null Counts
df.info() prints a concise summary: the index dtype, the number of non-null values per column, each column's dtype, and the total memory usage. Columns with fewer non-null values than the total row count have missing data. Object dtype often means a string column (or a mixed-type column) that may need cleaning before analysis.
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['A', 'B', None],
'score': [80.0, np.nan, 90.0],
'grade': ['A', 'C', 'A']})
df.info()
# Column Non-Null Count Dtype
# name 2 non-null object
# score 2 non-null float64
# grade 3 non-null objectdescribe(): Statistical Summary
df.describe() computes count, mean, std, min, 25th, 50th (median), 75th percentile, and max for all numeric columns. Pass include='all' to also summarise object (string) columns. Pass include='object' to only summarise categoricals. The output is itself a DataFrame, so you can save it or style it for a report.
import pandas as pd
df = pd.DataFrame({'age': [25, 30, 35, 40], 'score': [88, 72, 91, 65]})
print(df.describe().round(1))
# age score
# count 4.0 4.0
# mean 32.5 79.0
# std 6.5 12.0shape, ndim, and size
df.shape is a tuple (nrows, ncols). df.ndim always returns 2 for DataFrames. df.size is the total number of cells. Use len(df) for the number of rows (same as df.shape[0]). These are the simplest sanity checks — confirm the expected row count after loading and after each filtering step.
import pandas as pd
df = pd.DataFrame({'x': range(100), 'y': range(100), 'z': range(100)})
print(df.shape) # (100, 3)
print(len(df)) # 100
print(df.size) # 300
print(df.ndim) # 2dtypes and select_dtypes()
df.dtypes returns a Series mapping each column name to its dtype. df.select_dtypes(include='number') returns a new DataFrame with only numeric columns — useful for computing correlations or applying numeric transformations without accidentally operating on string columns. Pass exclude='object' to drop text columns.
import pandas as pd
df = pd.DataFrame({'a': [1,2], 'b': [1.0,2.0], 'c': ['x','y']})
print(df.dtypes)
# a int64
# b float64
# c object
numeric = df.select_dtypes(include='number')
print(numeric.columns.tolist()) # ['a', 'b']isnull() and Null Counts
df.isnull().sum() gives the count of missing values per column — the quickest way to find which columns need cleaning. Divide by len(df) to get the proportion of missing values. A column with more than 50% missing often needs special treatment: either drop it or apply domain-specific imputation.
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': [1, np.nan, 3], 'b': [np.nan, np.nan, 6]})
print(df.isnull().sum())
# a 1
# b 2
print((df.isnull().sum() / len(df)).round(2))
# a 0.33
# b 0.67value_counts() on a DataFrame Column
Calling df['col'].value_counts() lists the frequency of each unique value in that column, sorted by count. Add normalize=True for percentages. Use dropna=False to also count NaN as a category. This is the fastest way to check for category imbalance or unexpected values in a categorical column.
import pandas as pd
df = pd.DataFrame({'status': ['active','inactive','active','active','banned']})
print(df['status'].value_counts())
# active 3
# inactive 1
# banned 1
print(df['status'].value_counts(normalize=True).round(2))columns and index Inspection
df.columns.tolist() gives a plain Python list of column names — useful for programmatic manipulation or for logging. df.index shows the row labels and their type. Check df.index.is_unique to confirm no duplicate row labels exist, which is a prerequisite for many merge operations and time series calculations.
import pandas as pd
df = pd.DataFrame({'a': [1,2,3]}, index=['x', 'y', 'z'])
print(df.columns.tolist()) # ['a']
print(df.index.tolist()) # ['x', 'y', 'z']
print(df.index.is_unique) # Truesample() for Random Inspection
df.sample(n) returns n randomly selected rows without replacement. df.sample(frac=0.1) returns 10% of the rows. Set random_state= for reproducibility. Random sampling is better than head() for spotting issues in the middle of a large dataset that might not appear in the first few rows.
import pandas as pd
df = pd.DataFrame({'x': range(1000), 'y': range(1000)})
print(df.sample(3, random_state=42))
print(df.sample(frac=0.005, random_state=0)) # 0.5% of rowsmemory_usage() for Size Budgeting
df.memory_usage(deep=True) returns the memory used by each column in bytes. deep=True forces accurate measurement of object-dtype columns whose strings live outside the DataFrame buffer. Sum the values to get total memory. Use this before and after dtype downcasting to confirm you have reduced memory consumption.
import pandas as pd
df = pd.DataFrame({'a': range(10000), 'b': [str(i) for i in range(10000)]})
usage = df.memory_usage(deep=True)
print(usage)
print('Total bytes:', usage.sum())Quick Check
Test your understanding of basic DataFrame inspection from this lesson.
Lesson Recap
In this lesson you learned: head() and tail() let you visually inspect the start and end of a DataFrame, info() reveals dtypes and missing value counts in a single call, and describe() provides statistical summaries for numeric columns. Next up we load data from the most common file formats — CSV, Excel, and JSON — into DataFrames.
常见问题解答
「DataFrame 基础检查」课时是免费的吗?
是的 — 「DataFrame 基础检查」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Pandas & NumPy Academy 课程的其余内容,请升级到 CoddyKit PRO。 Pandas & NumPy Academy 课程共包含 4 节课。
「DataFrame 基础检查」这节课中我会学到什么?
使用 head()、tail()、info()、describe() 和 shape 快速了解任意 DataFrame 的内容和结构。 你通过在浏览器中直接运行的动手代码来练习 Pandas & NumPy Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Pandas & NumPy Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Pandas & NumPy Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「DataFrame 基础检查」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Pandas & NumPy Academy 课中编写并运行代码吗?
能。每节 Pandas & NumPy Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 创建 DataFrames
- 选择列与行
- 添加与删除列
- DataFrame 基础检查