简体   繁体   English

如何使用熊猫中的for循环根据另一列的条件填充一列中的缺失值?

[英]How to fill in missing values in one column based on a condition form another column using for loops in pandas?

weather_train=pd.DataFrame({
'site_id':[0,0,0,0,0,0,1,1,1,1,1],
'air_temperature': [25,22,'NaN',28,'NaN',30,45,'NaN',50,'Nan',24]
})
  • When site_id is 0, I need to calculate the mean air_temperature for site_id 0 and then use the mean to fill in the missing values for air_temperature in site_id 0.site_id是0,我需要计算的平均air_temperaturesite_id 0,然后用平均填补了缺失值air_temperaturesite_id 0。
  • Then, when the site_id is 1, I need to calculate the mean air_temperature for site_id 1 and fill in the missing values for air_temperature in site_id 1.然后,当site_id是1,我需要计算的平均air_temperature为SITE_ID在失踪值1和填充air_temperature在SITE_ID 1。

Have to do the same process for cloud_coverage .必须对cloud_coverage执行相同的过程。

Can anyone help me write a for loop in pandas for this?任何人都可以帮我在 Pandas 中为此编写一个 for 循环吗?

No need for loops.不需要循环。 Simply use groupby().transform() for inline mean aggregation enclosed in a conditional numpy.where :只需将groupby().transform()用于包含在条件numpy.where中的内联平均聚合:

weather_train['air_temperature'] = np.where(pd.isnull(weather_train['air_temperature']),
                                            weather_train.groupby(['site'])['air_temperature'].transform('mean'),    
                                            weather_train['air_temperature'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于另一列填充 Pandas 中的数字缺失值 - How to Fill Numeric missing Values In Pandas Based On Another Column 如何根据pandas中的列填充缺失值? - how to fill missing values based on column in pandas? 如何使用 pandas 根据列模式填充缺失值? - How to fill missing values based on column patterns using pandas? Python pandas 根据另一列的条件填充缺失值(NaN) - Python pandas fill missing value (NaN) based on condition of another column 如何根据 Pandas 中的条件将一列的值复制到另一列? - How to copy values of one column to another based on condition in Pandas? 如何根据 Pandas 数据框中的另一列值填充列中的缺失值? - How to fill missing values in a column based on another column values in a Pandas dataframe? 根据另一列(熊猫)的条件填充 null 个值 - Fill null values based on condition for another column (pandas) 根据pandas DataFrame中的另一列填充缺失值 - Fill missing values based on another column in a pandas DataFrame 如何根据 Pandas 中另一列的值填充一列的缺失值? - How to fill one column's missing values conditioning on another column's value in Pandas? 在 pandas 中,如何根据一列中的唯一值创建列,然后根据另一列中的值填充它? - In pandas, how do I create columns out of unique values in one column, and then fill it based on values in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM