<p>我认为你不应该忽视这样一个事实,即硬币的价值是在不同的时间计算的。你可以这样做:</p>
<pre><code>import pandas as pd
import hvplot.pandas
coins_best = {
'bitcoin': [[1603782192402, 13089.646908288987],
[1603865643028, 13712.070136258053]],
'ethereum': [[1603782053064, 393.6741989091851],
[1603865024078, 404.86117057956386]],
}
df_bitcoin = pd.DataFrame(data=coins_best['bitcoin'], columns=['timestamp', 'bitcoin'])
df_bitcoin['timestamp'] = pd.to_datetime(df_bitcoin['timestamp'], unit='ms')
df_ethereum = pd.DataFrame(data=coins_best['ethereum'], columns=['timestamp', 'ethereum'])
df_ethereum['timestamp'] = pd.to_datetime(df_ethereum['timestamp'], unit='ms')
df_coins = pd.concat([df_ethereum, df_bitcoin], sort=False)
</code></pre>
<p>您的<code>df_coins</code>现在将如下所示:</p>
<pre><code>+ + + + -+
| | timestamp | ethereum | bitcoin |
| + + + -|
| 0 | 2020-10-27 07:00:53.064000 | 393.674 | nan |
| 1 | 2020-10-28 06:03:44.078000 | 404.861 | nan |
| 0 | 2020-10-27 07:03:12.402000 | nan | 13089.6 |
| 1 | 2020-10-28 06:14:03.028000 | nan | 13712.1 |
+ + + + -+
</code></pre>
<p>现在,如果希望值在同一行上,可以使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html" rel="nofollow noreferrer">resampling</a>,这里我每天都这样做:一种硬币类型的同一天的所有值都是平均值:</p>
<pre><code>df_coins_resampled = df_coins.set_index('timestamp').resample('d').mean()
</code></pre>
<p><code>df_coins_resampled</code>将如下所示:</p>
<pre><code>+ -+ + -+
| timestamp | ethereum | bitcoin |
| -+ + -|
| 2020-10-27 00:00:00 | 393.674 | 13089.6 |
| 2020-10-28 00:00:00 | 404.861 | 13712.1 |
+ -+ + -+
</code></pre>
<p>我喜欢使用<a href="https://hvplot.holoviz.org/" rel="nofollow noreferrer">hvplot</a>获得结果的交互式绘图:</p>
<pre><code>df_coins_resampled.hvplot.scatter(
x='timestamp',
y=['bitcoin', 'ethereum'],
s=20, padding=0.1
)
</code></pre>
<p>结果图:
<a href="https://i.stack.imgur.com/bPHnR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bPHnR.png" alt="plotting bitcoins over time"/></a></p>