gpt4 book ai didi

python - 使用 pandasplot() 时出现意外振荡

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:08 24 4
gpt4 key购买 nike

我从网络上获取了一些数据并将其加载到 pandas 数据框中

import pandas as pd
%pylab inline

loc = 'https://blockchain.info/charts/hash-rate?showDataPoints=false&timespan=all&show_header=true&daysAverageString=1&scale=1&format=csv&address='
df = pd.read_csv(loc, parse_dates = True,
index_col = 0, skiprows = 1,
names = ['Date', 'Hash Rate (Gh/s)'])

然后我尝试使用 pandas df.plot 命令绘制它

df['Hash Rate (Gh/s)'].plot(logy = True)

我收到的图有意外的振荡 made with pandas

但是如果我用 matplotlib 绘制相同的数据

plt.semilogy(df['Hash Rate (Gh/s)'])

made with matplotlib

没有这些振荡。

我尝试使用 pandas 重新索引功能

df_idx = pd.date_range(df.index[0], df.index[-1])
df = df.reindex(df_idx, fill_value=nan)

但到目前为止还没有找到任何方法来消除图中的这些虚假振荡。如何消除这些振荡或在 pandas 中重新索引以消除它们?

最佳答案

您的日期未正确解析:它们先有天。如果您将 dayfirst=True 传递给 read_csv,它应该可以解决问题。

In [6]: df = pd.read_csv("ooo.csv", skiprows=1, names=['Date', 'Hash Rate (Gh/s)'], parse_dates=True, index_col=0, dayfirst=True)

In [7]: df.head(10)
Out[7]:
Hash Rate (Gh/s)
Date
2009-01-04 18:15:05 0.000000
2009-01-05 18:15:05 0.000000
2009-01-06 18:15:05 0.000000
2009-01-07 18:15:05 0.000000
2009-01-08 18:15:05 0.000000
2009-01-09 18:15:05 0.000696
2009-01-10 18:15:05 0.001541
2009-01-11 18:15:05 0.005269
2009-01-12 18:15:05 0.004424
2009-01-13 18:15:05 0.005717

[10 rows x 1 columns]

In [8]: !head ooo.csv
03/01/2009 18:15:05,0.00004971026962962963
04/01/2009 18:15:05,0.0
05/01/2009 18:15:05,0.0
06/01/2009 18:15:05,0.0
07/01/2009 18:15:05,0.0
08/01/2009 18:15:05,0.0
09/01/2009 18:15:05,0.0006959437748148148
10/01/2009 18:15:05,0.0015410183585185184
11/01/2009 18:15:05,0.005269288580740741
12/01/2009 18:15:05,0.004424213997037036

In [9]: df["Hash Rate (Gh/s)"].plot(logy=True)
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0xc4ea58c>

产生

figure w/o oscillations

关于python - 使用 pandasplot() 时出现意外振荡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306366/

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