gpt4 book ai didi

python - Pandas 过滤给出了一些奇怪的结果

转载 作者:行者123 更新时间:2023-12-01 05:12:16 25 4
gpt4 key购买 nike

我正在查看一些天气数据,其中包含每小时降水量的数据。我正在尝试整理包含降水量 > 0 的条目(范围从 0 到 0.4 左右)。

我以为我可以通过执行 weather[weather.HourlyPrecip > 0] 来获取这些(weather 这里是一个 DataFrame),但显然我错了:

fig, axes = plt.subplots(nrows=2, ncols=2)
weather[weather.HourlyPrecip > 0].HourlyPrecip.resample('1D', how='sum').plot(ax=axes[0,0]); axes[0,0].set_title('>0')
weather[weather.HourlyPrecip >= 0].HourlyPrecip.resample('1D', how='sum').plot(ax=axes[1,0]); axes[1,0].set_title('>=0')
weather[weather.HourlyPrecip == 0].HourlyPrecip.resample('1D', how='sum').plot(ax=axes[0,1]); axes[0,1].set_title('==0')
weather.HourlyPrecip.resample('1D', how='sum').plot(ax=axes[1,1]); axes[1,1].set_title('all')

...产量...

chart

这是怎么回事?

<小时/>

更新:以下是数据的大致情况:

In [215]: weather.HourlyPrecip
Out[215]: Date_Time
2013-12-01 00:51:00 0
2013-12-01 01:20:00 0
2013-12-01 01:51:00 0
2013-12-01 02:51:00 0
2013-12-01 03:10:00 0
2013-12-01 03:49:00 0
2013-12-01 03:51:00 0
2013-12-01 04:25:00 0
2013-12-01 04:35:00 0
2013-12-01 04:51:00 0
2013-12-01 05:51:00 0
2013-12-01 06:00:00 0
2013-12-01 06:09:00 0
2013-12-01 06:40:00 0
2013-12-01 06:51:00 0
...
2013-12-31 09:51:00 0
2013-12-31 10:51:00 0
2013-12-31 11:51:00 0
2013-12-31 12:51:00 0
2013-12-31 13:51:00 0
2013-12-31 14:51:00 0
2013-12-31 15:51:00 0
2013-12-31 16:51:00 0
2013-12-31 17:51:00 0
2013-12-31 18:51:00 0
2013-12-31 19:51:00 0
2013-12-31 20:51:00 0
2013-12-31 21:51:00 0
2013-12-31 22:51:00 0
2013-12-31 23:51:00 0
Name: HourlyPrecip, Length: 1018

以及所有值:

In [216]: np.unique(weather.HourlyPrecip.ravel())
Out[216]: array([ 0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08,
0.09, 0.1 , 0.12, 0.13, 0.19, 0.2 , 0.23, 0.24, 0.28, 0.38])

(该列全部为 float 。)

最佳答案

您最初的假设是正确的:df[df.precip > 0] 确实有效。这是你的测试有缺陷。当您重新采样时,当当天没有每小时数据时,您最终会得到一堆 np.nan 值。所以当你绘制它时,它看起来是脱节的。

尝试使用类似的东西:resampled_data.fillna(0).plot(),我想你会看到你所期望的。

关于python - Pandas 过滤给出了一些奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com