考试报名
考试报名
考试内容
考试大纲
在线客服
返回顶部

备考刷题,请到

CDA认证小程序

执行完以下代码后,df_filled 中索引为 2(第三行,原数据为 'forty', 5500)的那一行数据是什么? import pandas as pd import numpy as np df = pd.DataFrame({ 'age': ['25', '30', 'forty', '28', '-5', '150'], 'income': [5000, 6000, 5500, -2000, 7000, 8000] }) # 1. 清洗 age 列 def clean_age(val): try: v = int(val) if 0 <= v <= 120: return v else: return np.nan except: return np.nan df['age'] = df['age'].apply(clean_age) # 2. 清洗 income 列 df.loc[df['income'] < 0, 'income'] = np.nan df['income'] = df['income'].astype(float) # 3. 填充 df_filled = df.fillna(method='ffill') # 前向填充
A. age: NaN, income: 5500.0
B. age: 30.0, income: 5500.0
C. age: 40.0, income: 6000.0
D. age: NaN, income: 6000.0
上一题
下一题
收藏
点赞
评论
题目解析
题目评论(0)

索引 2 的行数据为:age=30.0, income=5500.0。 A 错误:忽略了前向填充。 B 正确:age 被前向填充为 30,income 保持原值。 C 错误:clean_age 函数无法将 'forty' 转换为 40,且 income 没有被填充的必要(原值存在)。 D 错误:age 会被填充,且 income 不需要填充。