【Pandas】pandas DataFrame head

Pandas2.2 DataFrame

Indexing, iteration

方法描述
DataFrame.head([n])用于返回 DataFrame 的前几行

pandas.DataFrame.head

pandas.DataFrame.head 是一个方法,用于返回 DataFrame 的前几行。这个方法非常有用,特别是在需要快速查看 DataFrame 的前几行数据时。

方法签名
DataFrame.head(n=5)
参数说明
  • n: 整数,默认为 5,表示要返回的行数。
返回值
  • 返回一个新的 DataFrame,包含前 n 行数据。
示例

假设有一个 DataFrame 如下:

import pandas as pd

data = {
    'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'B': [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10],
    'C': ['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
}

df = pd.DataFrame(data)
print("原始 DataFrame:")
print(df)

输出:

原始 DataFrame:
    A      B  C
0   1    1.1  x
1   2    2.2  y
2   3    3.3  z
3   4    4.4  a
4   5    5.5  b
5   6    6.6  c
6   7    7.7  d
7   8    8.8  e
8   9    9.9  f
9  10  10.10  g
示例1:返回前 5 行(默认参数)
df_head_default = df.head()
print("前 5 行 (默认参数):")
print(df_head_default)

结果:

前 5 行 (默认参数):
   A    B  C
0  1  1.1  x
1  2  2.2  y
2  3  3.3  z
3  4  4.4  a
4  5  5.5  b
示例2:返回前 3 行
df_head_3 = df.head(3)
print("前 3 行:")
print(df_head_3)

结果:

前 3 行:
   A    B  C
0  1  1.1  x
1  2  2.2  y
2  3  3.3  z
示例3:返回前 10 行
df_head_10 = df.head(10)
print("前 10 行:")
print(df_head_10)

结果:

前 10 行:
     A      B  C
0    1    1.1  x
1    2    2.2  y
2    3    3.3  z
3    4    4.4  a
4    5    5.5  b
5    6    6.6  c
6    7    7.7  d
7    8    8.8  e
8    9    9.9  f
9   10  10.10  g
示例4:返回前 0 行
df_head_0 = df.head(0)
print("前 0 行:")
print(df_head_0)

结果:

前 0 行:
Empty DataFrame
Columns: [A, B, C]
Index: []

通过这些示例,可以看到 pandas.DataFrame.head 方法如何返回 DataFrame 的前几行数据。

注意事项
  • head 方法默认返回 DataFrame 的前 5 行。
  • 可以通过参数 n 指定要返回的行数。
  • 如果 n 大于 DataFrame 的总行数,head 方法将返回所有行。
  • 如果 n 为 0,head 方法将返回一个空的 DataFrame。
示例代码及验证

为了验证 pandas.DataFrame.head 方法的效果,可以运行上述示例代码并查看输出结果。

import pandas as pd

# 创建一个示例 DataFrame
data = {
    'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'B': [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10],
    'C': ['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
}

df = pd.DataFrame(data)
print("原始 DataFrame:")
print(df)

# 返回前 5 行(默认参数)
df_head_default = df.head()
print("\n前 5 行 (默认参数):")
print(df_head_default)

# 返回前 3 行
df_head_3 = df.head(3)
print("\n前 3 行:")
print(df_head_3)

# 返回前 10 行
df_head_10 = df.head(10)
print("\n前 10 行:")
print(df_head_10)

# 返回前 0 行
df_head_0 = df.head(0)
print("\n前 0 行:")
print(df_head_0)
运行结果

运行上述代码后,你会看到以下输出:

原始 DataFrame:
    A      B  C
0   1    1.1  x
1   2    2.2  y
2   3    3.3  z
3   4    4.4  a
4   5    5.5  b
5   6    6.6  c
6   7    7.7  d
7   8    8.8  e
8   9    9.9  f
9  10  10.10  g

前 5 行 (默认参数):
   A    B  C
0  1  1.1  x
1  2  2.2  y
2  3  3.3  z
3  4  4.4  a
4  5  5.5  b

前 3 行:
   A    B  C
0  1  1.1  x
1  2  2.2  y
2  3  3.3  z

前 10 行:
     A      B  C
0    1    1.1  x
1    2    2.2  y
2    3    3.3  z
3    4    4.4  a
4    5    5.5  b
5    6    6.6  c
6    7    7.7  d
7    8    8.8  e
8    9    9.9  f
9   10  10.10  g

前 0 行:
Empty DataFrame
Columns: [A, B, C]
Index: []

通过这些示例,可以看到 pandas.DataFrame.head 方法如何返回 DataFrame 的前几行数据,并且如何使用不同的参数来控制返回的行数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuweidong0802

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值