
[英]Python Pandas replace NaN in one column with value from another column of the same row it has be as list column
[英]How to replace NaN value in one column based on the value of another column in the same row using Pandas?
有一个按类型(轿车、SUV、卡车等)、里程表、气缸、价格等分类的车辆数据集。我正在解决“气缸”列中的缺失值,其中包含发动机中的气缸数车辆。 我填写缺失值的方法是使用每种车辆类型的气缸中位数。 使用 pivot 表,它看起来像这样: pivot 表的屏幕截图
现在我想创建一个遍历每一行的 for 循环,当它在“圆柱”列中找到 NaN 值时,根据类型将其替换为 pivot 表中的中值。
谢谢
因此,您有一个 for 循环遍历汽车 dataframe 中的每一行,当它找到 NaN 值时,它将查看您的 pivot_table 并将 NaN 替换为该特定汽车类型的 Cylinders 值。
for index, row in cars_table.iterrows():
if pd.isnull(row['Cylinders']):
pivot_table_index = pivot_table.index.get_loc(row['Type'])
cars_table.loc[index, 'Cylinders'] = pivot_table['Cylinders'][pivot_table_index]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.