【问题标题】:Plotting a Pandas Framework from a CSV File从 CSV 文件绘制 Pandas 框架
【发布时间】:2017-12-24 16:47:30
【问题描述】:

大家早上好,

基本上,我正在为我居住的彩票中的中奖人数做一个数据框。我一直在绘制从 CSV 文件创建的数据框。这是我的代码:

    data_file = pd.read_csv('Jugadas_Ganadas.csv')
    data_file.set_index('Date', inplace=True)
    df = pd.DataFrame(data_file)
    pickle_out = open('Pega3_Ganados.pickle', 'wb')
    pickle.dump(df, pickle_out)
    pickle_out.close()

    fi = plt.figure()
    ax1 = plt.subplot2grid((1, 1), (0, 0))


    data_analysis = pd.read_pickle('Pega3_Ganados.pickle')

    print data_analysis

打印数据分析后,我得到以下框架。

               Values
    Date             
    15/07/2017  [660]
    16/07/2017   [40]
    17/07/2017  [300]
    18/07/2017   [40]
    19/07/2017   [80]

在我确认我的变量中有一个框架后,我尝试使用此脚本将它绘制在 matplotlib 图中。

    fi = plt.figure()
    ax1 = plt.subplot2grid((1, 1), (0, 0))

    data_analysis['Values'].plot(ax=ax1, label='Ganadas')
    plt.legend(loc=4)
    plt.show()

但是,我收到以下错误:

TypeError: Empty 'DataFrame': no numeric data to plot

【问题讨论】:

    标签: python python-2.7 pandas csv matplotlib


    【解决方案1】:
    print (type(df.loc[df.index[0], 'Values']))
    <class 'str'>
    
    data_analysis['Values'].str.strip('[]').astype(int).plot(ax=ax1, label='Ganadas')
    

    我也应该在我的代码中尝试一下。

    【讨论】:

      【解决方案2】:

      我认为values 列包含列表,因此需要通过str[0] 选择列表的第一个值:

      print (type(df.loc[df.index[0], 'Values']))
      <class 'list'>
      
      data_analysis['Values'].str[0].plot(ax=ax1, label='Ganadas')
      

      如果值的类型是string,则使用stripastype

      print (type(df.loc[df.index[0], 'Values']))
      <class 'str'>
      
      data_analysis['Values'].str.strip('[]').astype(int).plot(ax=ax1, label='Ganadas')
      

      【讨论】:

      • 谢谢jezrael,是的,它是一个几乎没有注意到它的字符串。我是使用 Pandas 框架的新手,所以我仍在学习 Pandas 的语法。再次感谢您的帮助!
      • 我已经做到了!我是stackoverflow的新手,但我确实单击了复选标志。非常感谢您的帮助。
      • 只是为了确认在这种情况下是什么问题。主要问题是它无法绘制框架,因为数据类型是字符串,并且由于我使用 astype 方法将行转换为整数,因此它识别了数据类型并且能够将其绘制成图表。
      猜你喜欢
      • 2016-07-03
      • 2021-12-15
      • 1970-01-01
      • 2015-04-29
      • 2013-11-28
      • 1970-01-01
      • 2018-11-16
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多