【问题标题】:Create a dataframe from a list从列表创建数据框
【发布时间】:2016-02-29 00:24:50
【问题描述】:

我有一个学习器,它返回与日期对应的值列表。

我需要该函数返回一个数据框以用于绘图。我已经创建了数据框,但现在我需要使用列表中的值填充数据框。这是我的代码:

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

【问题讨论】:

    标签: list pandas dataframe


    【解决方案1】:

    您可以简单地使用以下方法创建数据框:

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

    【讨论】:

      【解决方案2】:

      我认为您可以使用DataFrame 的参数data。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 2020-11-15
        • 2020-02-23
        • 1970-01-01
        • 2021-12-22
        相关资源
        最近更新 更多