简体   繁体   English

如何使用 Pandas 根据同一行中另一列的值替换一列中的 NaN 值?

[英]How to replace NaN value in one column based on the value of another column in the same row using Pandas?

There is a dataset of vehicles by type (sedan, SUV, truck, etc), odometer, cylinders, price, etc. I am addressing the missing values in the column 'cylinders', which contains the number of cylinders in the engine of the vehicle.有一个按类型(轿车、SUV、卡车等)、里程表、气缸、价格等分类的车辆数据集。我正在解决“气缸”列中的缺失值,其中包含发动机中的气缸数车辆。 My approach to fill in the missing values is to use the median number of cylinders per type of vehicle.我填写缺失值的方法是使用每种车辆类型的气缸中位数。 Using a pivot table it looks like this: Screenshot of the pivot table使用 pivot 表,它看起来像这样: pivot 表的屏幕截图

Now I want to create a for loop that goes through every row and when it finds a NaN value in column 'cylinders' replaces it with the median value seen in the pivot table according to the type.现在我想创建一个遍历每一行的 for 循环,当它在“圆柱”列中找到 NaN 值时,根据类型将其替换为 pivot 表中的中值。

Thanks谢谢

So there you have a for loop that goes through every row in your cars dataframe and when it finds a NaN value its gonna look in your pivot_table and will replace the NaN with the Cylinders value of that particular car type.因此,您有一个 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.

相关问题 Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column Python Pandas 用另一列下一行的值替换一列中的 NaN - Python Pandas replace NaN in one column with value from a row below of another column 根据另一列中的值,用字符串替换一列中的NaN - Replace NaN's in one column with string, based on value in another column 使用 Python Pandas,仅当“nan”值不存在时,我可以根据另一列替换 df 中一列的值吗? - Using Python Pandas, can I replace values of one column in a df based on another column only when a "nan" value does not exist? 如何根据另一列中的值用另一列的平均值替换 NaN 值? Pandas - How to replace NaN values with another column's mean based on value in another column? Pandas 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? 如何根据同一 dataframe 中另一列的值替换 Dataframe 中列中的 NaN 值 - How to replace NaN value in column in Dataframe based on values from another column in same dataframe 在Pandas中,如何根据另一行中的另一列值更新一行中的列值 - In Pandas how to update column value in one row based on another column value in another row 如果特定列的一个值为 NaN,有没有办法使用 ffill 替换整个 pandas dataframe 行? - Is there a way to replace a whole pandas dataframe row using ffill, if one value of a specific column is NaN? Pandas 用基于另一列的第一个非 nan 值替换 nan - Pandas replace nan with first non-nan value based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM