Python-数据框-Matplotly-KeyError:“ [列]中[Int64Index / Datetime…]都不存在

问题描述 投票:0回答:1

这是我在stackoverflow中的一个非常问题,即使我习惯于在被卡住时仍在寻找解决方案。但是,今天,我真的很困,不知道如何处理。

简而言之,我编写了一个脚本来收集比特币的价格并将其存储在mySQL DB中。另一个python脚本进入数据库收集数据,我想使用matplotlib库生成图表(折线)。

我想使用以下代码基于日期时间创建图形

        sql = "SELECT * FROM t_btc_2 WHERE id_btc >= 3800;"
        mycursor.execute(sql)
        table_rows = mycursor.fetchall()
        df = pd.DataFrame(table_rows)
        mycursor.close()
        df.rename(columns={0: 'id_BTC', 1:'Date_and_Time', 2:'BTC_Value'}, inplace=True)
        ax = plt.gca()
        df.plot(kind='line', x=df['Date_and_Time'], y='BTC_Value', color='red', ax=ax)
        df.plot()
        plt.show()

但出现以下错误:

KeyError: "None of [DatetimeIndex(['2020-01-26 22:08:44', '2020-01-26 22:08:55',\n               '2020-01-26 22:09:05', '2020-01-26 22:09:15',\n
     '2020-01-26 22:09:27', '2020-01-26 22:09:37',\n               '2020-01-26 22:09:45', '2020-01-26 22:09:57',\n               '2020-01-26 22:10:09', '2020-01-26 22:10:19',\n               '2020-01-26 22:10:28', '2020-01-26 22:10:38',\n               '2020-01-26 22:10:49', '2020-01-26 22:11:00',\n        
       '2020-01-26 22:11:08', '2020-01-26 22:11:18',\n               '2020-01-26 22:11:29', '2020-01-26 22:11:41',\n               '2020-01-26 22:11:51', 
'2020-01-26 22:12:02',\n               '2020-01-26 22:12:12', '2020-01-26 22:12:21',\n               '2020-01-26 22:12:31', '2020-01-26 22:12:42',\n      
         '2020-01-26 22:12:52', '2020-01-26 22:13:03',\n               '2020-01-26 22:13:13', '2020-01-26 22:13:23',\n               '2020-01-26 22:13:35', '2020-01-26 22:13:45',\n               '2020-01-26 22:13:56', '2020-01-26 22:14:06',\n               '2020-01-26 22:14:17', '2020-01-26 22:14:27',\n    
           '2020-01-26 22:14:36', '2020-01-26 22:14:49',\n               '2020-01-26 22:14:57', '2020-01-26 22:15:10',\n               '2020-01-26 22:15:19'],\n              dtype='datetime64[ns]', freq=None)] are in the [columns]"

我基于主键,尝试了以下代码:

        sql = "SELECT * FROM t_btc_2 WHERE id_btc >= 3800;"
        mycursor.execute(sql)
        table_rows = mycursor.fetchall()
        df = pd.DataFrame(table_rows)
        mycursor.close()
        df.rename(columns={0: 'id_BTC', 1:'Date_and_Time', 2:'BTC_Value'}, inplace=True)
        ax = plt.gca()
        df.plot(kind='line', x=df['id_BTC'], y='BTC_Value', color='red', ax=ax)
        df.plot()
        plt.show()

我遇到以下错误:

KeyError: "None of [Int64Index([3800, 3801, 3802, 3803, 3804, 3805, 3806, 3807, 3808, 3809, 3810,\n            3811, 3812, 3813, 3814, 3815, 3816, 3817, 3818, 3819, 3820, 3821,\n            3822, 3823, 3824, 3825, 3826, 3827, 3828, 3829, 3830, 3831, 3832,\n            3833, 3834, 3835, 3836, 3837, 3838],\n 
          dtype='int64')] are in the [columns]"

我的SQL查询结果提供了以下结果:

    id_BTC       Date_and_Time BTC_Value
0     3800 2020-01-26 22:08:44   8636.96
1     3801 2020-01-26 22:08:55   8636.96
2     3802 2020-01-26 22:09:05   8626.76
3     3803 2020-01-26 22:09:15   8637.24
4     3804 2020-01-26 22:09:27   8626.77
5     3805 2020-01-26 22:09:37   8637.24
6     3806 2020-01-26 22:09:45   8637.24
7     3807 2020-01-26 22:09:57   8634.99
8     3808 2020-01-26 22:10:09   8634.99
9     3809 2020-01-26 22:10:19   8634.15
10    3810 2020-01-26 22:10:28   8634.15
11    3811 2020-01-26 22:10:38   8635.00
12    3812 2020-01-26 22:10:49   8635.00
13    3813 2020-01-26 22:11:00   8635.00
14    3814 2020-01-26 22:11:08   8634.99
15    3815 2020-01-26 22:11:18   8625.11
16    3816 2020-01-26 22:11:29   8625.10
17    3817 2020-01-26 22:11:41   8634.99
18    3818 2020-01-26 22:11:51   8634.99
19    3819 2020-01-26 22:12:02   8625.10
20    3820 2020-01-26 22:12:12   8620.58
21    3821 2020-01-26 22:12:21   8633.80
22    3822 2020-01-26 22:12:31   8633.80
23    3823 2020-01-26 22:12:42   8633.80
24    3824 2020-01-26 22:12:52   8619.37
25    3825 2020-01-26 22:13:03   8619.37
26    3826 2020-01-26 22:13:13   8619.37
27    3827 2020-01-26 22:13:23   8631.41
28    3828 2020-01-26 22:13:35   8617.98
29    3829 2020-01-26 22:13:45   8617.98
30    3830 2020-01-26 22:13:56   8617.78
31    3831 2020-01-26 22:14:06   8611.47
32    3832 2020-01-26 22:14:17   8611.47
33    3833 2020-01-26 22:14:27   8611.47
34    3834 2020-01-26 22:14:36   8611.47
35    3835 2020-01-26 22:14:49   8618.88
36    3836 2020-01-26 22:14:57   8618.88
37    3837 2020-01-26 22:15:10   8614.13
38    3838 2020-01-26 22:15:19   8627.51

是否有生成预期图表的方法?我想了解请求的时间范围内价格的变化情况。

感谢您的时间和帮助。

汤姆

python pandas dataframe
1个回答
0
投票

Pandas plot()方法的x和y参数仅需要将列名作为字符串。在您的代码中,您正在混合传递给这些参数的对象的类型。还有两个地方都有一个冗余的df.plot()。下面的代码有效。请确保已使用pd.to_datetime()将Date_and_Time列转换为熊猫的datetime类型。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create a toy data frame
rng = pd.date_range('1/5/2018 00:00', periods=5, freq='10M')
df = pd.DataFrame({'id_BTC': np.arange(5) + 3000,
                   'BTC_Value': np.random.randint(8000, 9000, 5),
                   'Date_and_Time': pd.date_range('20200101', periods=5,
                                                  freq='T')})

fig, ax = plt.subplots()
df.plot(kind='line', x='id_BTC', y='BTC_Value', color='red', ax=ax)
plt.show()

fig, ax = plt.subplots()
df.plot(kind='line', x='Date_and_Time', y='BTC_Value', color='red', ax=ax)
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.