get one value of a list nested in a dictionary to create a dataframe update 1

I think you shouldn’t ignore the fact that values of coins are taken at different times. You could do something like this:

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)

Your df_coins will now look like this:

+----+----------------------------+------------+-----------+
|    | 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 |
+----+----------------------------+------------+-----------+

Now if you want values to be on the same line, you could use resampling, here I do it per day: all values of the same day for a coin type are averaged:

df_coins_resampled = df_coins.set_index('timestamp').resample('d').mean()

df_coins_resampled will look like this:

+---------------------+------------+-----------+
| timestamp           |   ethereum |   bitcoin |
|---------------------+------------+-----------|
| 2020-10-27 00:00:00 |    393.674 |   13089.6 |
| 2020-10-28 00:00:00 |    404.861 |   13712.1 |
+---------------------+------------+-----------+

I like to use hvplot to get an interactive plot of the result:

df_coins_resampled.hvplot.scatter(
    x='timestamp', 
    y=['bitcoin', 'ethereum'], 
    s=20, padding=0.1
)

Resulting plot: plotting bitcoins over time

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top