在 Pandas 中刪除 DataFrame 的前三行


若要刪除 Pandas 中 DataFrame 的前三行,我們可以使用 iloc() 方法。

步驟

  • 建立二維、大小可變、可能異構的表格資料,df
  • 列印輸入 DataFrame,df
  • 使用 df.iloc[3:] 刪除前三行。
  • 列印更新後的 DataFrame。

示例

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 7, 0, 7, 0, 5, 2],
      "y": [4, 7, 5, 1, 5, 1, 4, 7],
      "z": [9, 3, 5, 1, 5, 1, 9, 3]
   }
)

print "Input DataFrame is:\n", df
df = df.iloc[3:]
print "After deleting the first 3 rows: \n", df

輸出

Input DataFrame is:
   x  y  z
0  5  4  9
1  2  7  3
2  7  5  5
3  0  1  1
4  7  5  5
5  0  1  1
6  5  4  9
7  2  7  3

After deleting the first 3 rows:
   x  y  z
3  0  1  1
4  7  5  5
5  0  1  1
6  5  4  9
7  2  7  3

更新於: 14-Sep-2021

1000 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始學習
廣告