始める無料で始める

DataFrame への高度な変換の適用

pandas には多数の組み込み変換ツールが用意されていますが、変換処理によってはより高度なロジックが必要になる場合があります。apply 関数を使うと、DataFrame の行や列にユーザー定義関数を適用でき、高度な変換や特徴量の生成が可能になります。

find_street_name() 関数は、"street_address" から番地部分の数字を取り除き、通り名を抽出します。この関数はすでにメモリに読み込まれており、raw_testing_scores DataFrame に適用する準備ができています。

この演習はコースの一部です

Python で学ぶ ETL と ELT

コースを見る

演習の手順

  • transform() 関数の定義内で find_street_name() 関数を使い、"street_name" という新しい列を作成します。
  • transform() 関数を使って raw_testing_scores DataFrame をクレンジングします。
  • cleaned_testing_scores DataFrame の先頭部分を表示し、新しく追加された "street_name" 列を確認します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

def transform(raw_data):
	# Use the apply function to extract the street_name from the street_address
    raw_data["street_name"] = raw_data.____(
   		# Pass the correct function to the apply method
        ____,
        axis=1
    )
    return raw_data

# Transform the raw_testing_scores DataFrame
cleaned_testing_scores = ____(raw_testing_scores)

# Print the head of the cleaned_testing_scores DataFrame
print(cleaned_testing_scores.____())
コードを編集して実行