我有一个名为df_mod
的熊猫数据框。此数据帧中感兴趣的一个变量称为Evap_mod
。当我使用命令print(df_mod['Evap_mod'])
时,它返回:
2003-12-20 00:30:00 1.930664
2003-12-21 00:30:00 1.789290
2003-12-22 00:30:00 2.318347
2003-12-23 00:30:00 1.741943
2003-12-24 00:30:00 1.686124
2003-12-25 00:30:00 1.852876
2003-12-26 00:30:00 1.759650
2003-12-27 00:30:00 1.566521
2003-12-28 00:30:00 1.496039
2003-12-29 00:30:00 1.540751
2003-12-30 00:30:00 2.006475
2003-12-31 00:30:00 1.920912
Name: Evap_mod, Length: 729, dtype: float32
我还有另一个名为dff
的熊猫数据框。此数据帧中感兴趣的一个变量称为PET_PT
。当我使用命令print(dff['PET_PT'])
时,它返回:
2003-12-20 4.810697
2003-12-21 4.739378
2003-12-22 4.994467
2003-12-23 5.138086
2003-12-24 5.024226
2003-12-25 4.937206
2003-12-26 4.551416
2003-12-27 NaN
2003-12-28 NaN
2003-12-29 NaN
2003-12-30 NaN
2003-12-31 NaN
Freq: D, Name: PET_PT, Length: 729, dtype: float64
我想在这两个变量之间进行以下简单计算:
df_mod['ER_mod']=(df_mod['Evap_mod']+np.mean(ddf['PET_PT']))/(ddf['PET_PT']+np.mean(ddf['PET_PT']))
不幸的是,此计算仅返回NaN:
2003-12-20 00:30:00 NaN
2003-12-21 00:30:00 NaN
2003-12-22 00:30:00 NaN
2003-12-23 00:30:00 NaN
2003-12-24 00:30:00 NaN
2003-12-25 00:30:00 NaN
2003-12-26 00:30:00 NaN
2003-12-27 00:30:00 NaN
2003-12-28 00:30:00 NaN
2003-12-29 00:30:00 NaN
2003-12-30 00:30:00 NaN
2003-12-31 00:30:00 NaN
Name: ER_mod, Length: 729, dtype: float64
有人知道为什么它会返回NaN以及如何解决此问题吗?
答案 0 :(得分:1)
原因是不同的索引值,因此在划分索引值后不匹配并创建了NaN
s。
解决方案是map
系列ddf['PET_PT']
的{{1}系列,由DatetimeIndex.normalize
创建的辅助列date
用于删除时间,并且还使用了熊猫mean
的功能:
#same index values like df_mod
new = df_mod.assign(date = df_mod.index.normalize())['date'].map(ddf['PET_PT'])
print (new)
2003-12-20 00:30:00 4.810697
2003-12-21 00:30:00 4.739378
2003-12-22 00:30:00 4.994467
2003-12-23 00:30:00 5.138086
2003-12-24 00:30:00 5.024226
2003-12-25 00:30:00 4.937206
2003-12-26 00:30:00 4.551416
2003-12-27 00:30:00 NaN
2003-12-28 00:30:00 NaN
2003-12-29 00:30:00 NaN
2003-12-30 00:30:00 NaN
2003-12-31 00:30:00 NaN
Name: date, dtype: float64
df_mod['ER_mod']= df_mod['Evap_mod'] + ddf['PET_PT'].mean())/(new+ddf['PET_PT'].mean()
print (df_mod)
Evap_mod ER_mod
2003-12-20 00:30:00 1.930664 0.702960
2003-12-21 00:30:00 1.789290 0.693480
2003-12-22 00:30:00 2.318347 0.729125
2003-12-23 00:30:00 1.741943 0.661170
2003-12-24 00:30:00 1.686124 0.663134
2003-12-25 00:30:00 1.852876 0.685986
2003-12-26 00:30:00 1.759650 0.704152
2003-12-27 00:30:00 1.566521 NaN
2003-12-28 00:30:00 1.496039 NaN
2003-12-29 00:30:00 1.540751 NaN
2003-12-30 00:30:00 2.006475 NaN
2003-12-31 00:30:00 1.920912 NaN
如果长度DataFrame
相同且仅inde值不同,则可以将一个索引重新分配给另一个:
ddf.index = df_mod.index
df_mod['ER_mod'] = (df_mod['Evap_mod'] + ddf['PET_PT'].mean())/\
(ddf['PET_PT'] + ddf['PET_PT'].mean())
print (df_mod)
Evap_mod ER_mod
2003-12-20 00:30:00 1.930664 0.702960
2003-12-21 00:30:00 1.789290 0.693480
2003-12-22 00:30:00 2.318347 0.729125
2003-12-23 00:30:00 1.741943 0.661170
2003-12-24 00:30:00 1.686124 0.663134
2003-12-25 00:30:00 1.852876 0.685986
2003-12-26 00:30:00 1.759650 0.704152
2003-12-27 00:30:00 1.566521 NaN
2003-12-28 00:30:00 1.496039 NaN
2003-12-29 00:30:00 1.540751 NaN
2003-12-30 00:30:00 2.006475 NaN
2003-12-31 00:30:00 1.920912 NaN
答案 1 :(得分:1)
您的列中包含缺少的数据,因此您应根据目标使用不同的方法(平均值,零,中位数,随机等)来估算值
答案 2 :(得分:1)
此处pandas
和numpy
的行为有所不同。每当计算np.mean(x)
时,如果x
包含NaN
,在处理大熊猫NaN
时,您将得到NaN
的结果。以下应该可以工作
df_mod['ER_mod'] = (df_mod['Evap_mod'] + ddf['PET_PT'].mean())/\
(ddf['PET_PT'] + ddf['PET_PT'].mean())
否则,您可以使用np.nanmean
代替np.mean
。