繁体   English   中英

DataFrame熊猫显示NAN

[英]DataFrame Pandas shows NAN

我有hdf5,已经移到DataFrame,但是问题是当我要绘制时,图形上没有任何显示。 而且我检查了新的数据框,但我发现没有任何东西。 这是我的DF( 不允许发布图片,因此请单击链接

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

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

我认为您需要从Timestamp列中选择set_index ,然后选择Price和plot列:

#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()

并获得NaNs ,如果使用DataFrame的构造,因为数据不对齐-的指数值df不一样的Timestamp列。

您可以通过添加.values来做到这一点,而如何创建一个系列呢?

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

看到这里回答了: pandas.Series()使用DataFrame列创建将返回NaN数据条目

完整示例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM