对 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_scoresDataFrame。 - 打印
cleaned_testing_scoresDataFrame 的表头,查看新增的"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.____())