DataFrame熊猫显示NAN
问题描述:
我有hdf5和我已经移动到DataFrame,但问题是当我想绘图,什么也没有显示在图上。我检查了新的数据框,但我看到,没有任何东西。 这是我的DF( I don't allowed to post pics, so please click to the link)DataFrame熊猫显示NAN
df1 = pd.DataFrame(df.Price, index = df.Timestamp)
plt.figure()
df1.plot()
plt.show()
二DF显示价格列NAN。怎么了?
答
我认为你从列Timestamp
,选择列Price
和情节需要set_index
:
#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() 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
我认为它会改变第一个DataFrame。我不需要在第一个数据帧上进行更改。但是不要删除这段代码,它可以对其他人有所帮助 –
如果没有分配输出,则没有变化。 – jezrael
空'DataFrame':无数字数据绘图 我收到这种错误 –