[英]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]
})
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_temperature
的site_id
0,然后用平均填补了缺失值air_temperature
在site_id
0。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.