df_new_last.iloc[:,-1]与df_new_last.iloc[:,:-1]

df_new_last.iloc[:,-1]df_new_last.iloc[:,:-1] 这两个表达式在 Pandas 中用于选择 DataFrame df_new_last 中的数据,但它们选择的内容有所不同:

  1. df_new_last.iloc[:,-1]

    • 这个表达式用于选择 DataFrame 中的最后一列。
    • : 表示选择所有行。
    • -1 表示选择最后一列(因为 Python 的索引是从 0 开始的,所以 -1 就是最后一个索引)。
    • 结果将是一个 Series 对象,包含了 DataFrame 最后一列的所有数据。
  2. df_new_last.iloc[:,:-1]

    • 这个表达式用于选择 DataFrame 中除了最后一列之外的所有列。
    • : 表示选择所有行。
    • -1: 表示从开始到倒数第一列(不包括最后一列)的所有列。
    • 结果将是一个 DataFrame 对象,包含了除了最后一列之外的所有列的数据。

以下面的 DataFrame df_new_last 为例:

A B C D
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
  • df_new_last.iloc[:,-1] 将会返回最后一列(D列)的数据:
0    4
1    8
2   12
Name: D, dtype: int64
  • df_new_last.iloc[:,:-1] 将会返回除了最后一列之外的所有列(A、B、C列)的数据:
   A   B   C
0  1   2   3
1  5   6   7
2  9  10  11

总结来说,df_new_last.iloc[:,-1] 返回最后一列,而 df_new_last.iloc[:,:-1] 返回除了最后一列之外的所有列。

猜你喜欢

转载自blog.csdn.net/2301_81133727/article/details/143467158
df