2879. Display the First Three Rows

使用 Python 的套件 Pandas 產生表。
這題只有 Python 的語言選項。

想法:

  1. 篩選出頭幾行
  2. 回傳時需要 Pandas DataFrame 型別

之前主要卡在回傳型別這裡…它一直認為我回傳的型別是 Nontype…

總之結果:

import pandas as pd  
dataframe = pd.read_csv('./employees.csv')

def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
	return pd.DataFrame(employees.head(3))

print(selectFirstRows(dataframe))
Python

(其中 employees.csv 檔案是用來測試回傳結果用)

打完收工。

Written with StackEdit.

Comments