简体   繁体   中英

DataFrame Pandas shows NAN

I have hdf5 and I have moved to DataFrame, but problem is when I want to plot, nothing shows on the graph. And I have checked new dataframe, but I saw, there was nothing. This is my 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()

Second DF shows NAN in price column. Whats wrong?

I think you need set_index from column Timestamp , select column Price and 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()

And get NaNs if use DataFrame constructor, because data not aligned - values of index of df are not same as Timestamp column.

You can do this by adding .values, And how about creating a series instead?

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

Saw it was answered here: pandas.Series() Creation using DataFrame Columns returns NaN Data entries

Full example:

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

在此处输入图片说明


Difference

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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