【问题标题】:DataFrame Pandas shows NANDataFrame Pandas 显示 NAN
【发布时间】:2018-01-14 16:59:11
【问题描述】:

我有 hdf5 并且已移至 DataFrame,但问题是当我想绘图时,图表上没有显示任何内容。我检查了新的数据框,但我看到了,什么都没有。 这是我的 DF ( I don't allowed to post pics, so please click to the link)

df1 = pd.DataFrame(df.Price, index = df.Timestamp)
plt.figure()
df1.plot()
plt.show()

第二个 DF 在价格列中显示 NAN。怎么了?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    我认为您需要set_index 从列Timestamp,选择列Price 并绘制:

    #convert column to floats
    df['Price'] = df['Price'].astype(float)
    df.set_index('Timestamp')['Price'].plot()
    

    #if some non numeric data, convert them to NaNs
    df['Price'] = pd.to_numeric(df['Price'], errors='coerce')
    df.set_index('Timestamp')['Price'].plot()
    

    如果使用 DataFrame 构造函数,则获取 NaNs,因为数据未对齐 - df 的索引值与 Timestamp 列不同。

    【讨论】:

    • 我认为它会改变第一个 DataFrame。我不需要对第一个数据框进行更改。但不要删除此代码,它可能对其他人有帮助
    • 如果没有分配输出就没有变化。
    • Empty 'DataFrame': no numeric data to plot 我收到这种错误
    【解决方案2】:

    您可以通过添加 .values 来做到这一点,那么创建一个系列怎么样?

    #df1 = pd.DataFrame(df.Price.values, df.Timestamp)
    serie = pd.Series(df.Price.values, df.Timestamp)
    

    看到这里回答了:pandas.Series() Creation using DataFrame Columns returns NaN Data entries

    完整示例:

    import pandas as pd
    import numpy as np
    import datetime
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame(columns=["Price","Timestamp","Random"])
    df.Price = np.random.randint(100, size = 10)
    df.Timestamp = [datetime.datetime(2000,1,1) + \
                datetime.timedelta(days=int(i)) for i in np.random.randint(100, size = 10)]
    df.Random = np.random.randint(10, size= 10)
    
    serie = pd.Series(df.Price.values, df.Timestamp)
    
    serie.plot()
    plt.show()
    


    区别

    print("{}\n{}".format(type(df.Price), type(df.Price.values)))
    <class 'pandas.core.series.Series'> # does not work
    <class 'numpy.ndarray'> # works
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2015-02-14
      • 2018-11-14
      • 2021-06-20
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      相关资源
      最近更新 更多