Chris
Chris

Reputation: 31206

Create a dataframe from a list

I've got a learner that returns a list of values corresponding to dates.

I need the function to return a dataframe for plotting purposes. I've got the dataframe created, but now I need to populate the dataframe with the values from the list. Here is my code:

learner.addEvidence(x,y_values.values)

y_prediction_list = learner.query(x) # this yields a plain old python list

y_prediction_df = pd.DataFrame(index=dates,columns="Y-Prediction")
y_prediction_df = ??

return y_prediction_df

Upvotes: 3

Views: 1809

Answers (2)

jezrael
jezrael

Reputation: 862641

I think you can use parameter data of DataFrame.

import pandas as pd

#test data
dates = ["2014-05-22 05:37:59", "2015-05-22 05:37:59","2016-05-22 05:37:59"]
y_prediction_list = [1,2,3]

y_prediction_df = pd.DataFrame(data=y_prediction_list, index=dates,columns=["Y-Prediction"])
print y_prediction_df
#                     Y-Prediction
#2014-05-22 05:37:59             1
#2015-05-22 05:37:59             2
#2016-05-22 05:37:59             3

print y_prediction_df.info()
#<class 'pandas.core.frame.DataFrame'>
#Index: 3 entries, 2014-05-22 05:37:59 to 2016-05-22 05:37:59
#Data columns (total 1 columns):
#Y-Prediction    3 non-null int64
#dtypes: int64(1)
#memory usage: 48.0+ bytes
#None

Upvotes: 1

Samuelliyi
Samuelliyi

Reputation: 68

you can simply create the dataframe with:

y_prediction_df=pd.DataFrame({"Y-Prediction":y_prediction_list},index=dates)

Upvotes: 2

Related Questions