【问题标题】:How to add values from a list and a corresponding for loop to a dataframe如何将列表中的值和相应的 for 循环添加到数据框
【发布时间】:2022-08-10 17:31:48
【问题描述】:

我有一个变量列表如下:

variables = [\'positional_versatility\', \'weak_foot\', \'skill_moves\']

然后我有一个 for 循环,它打印每个变量的系数:

for (v, c) in zip(variables, model.coef_):
    print(\"%s has coefficient %5.4f\" % (v, c))

哪个输出:

positional_versatility has coefficient 0.0041
weak_foot has coefficient -0.0421
skill_moves has coefficient 0.0010

我的问题:如何将这些系数值及其相应的变量名称添加到数据框中?

    标签: python pandas dataframe for-loop


    【解决方案1】:

    使用DataFrame 构造函数:

    df = pd.DataFrame({'a':variables, 'b': model.coef_})
    

    【讨论】:

      【解决方案2】:

      利用:

      df = pd.DataFrame([dict(zip(variables, model.coef_))])
      

      【讨论】:

        猜你喜欢
        • 2011-10-02
        • 2019-03-02
        • 1970-01-01
        • 2021-02-01
        • 1970-01-01
        • 2020-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多