Create column named “Id” based on row index

后端 未结 2 1439
庸人自扰
庸人自扰 2021-01-13 16:50

I would like to create a new column for my dataframe named \"Id\" where the value is the row index +1. I would like to be like the example below:

   ID  Col         


        
相关标签:
2条回答
  • 2021-01-13 17:34

    You can add one to the index and assign it to the id column:

    df = pd.DataFrame({"Col1": list("abc")})
    
    df["id"] = df.index + 1
    
    df
    #Col1   id
    #0  a    1
    #1  b    2
    #2  c    3
    
    0 讨论(0)
  • 2021-01-13 17:34

    I converted the prediction list to an array then created an dataframe with group totals and plotted the dataframe

    y_pred=model.predict(X_test)
    
    #print(y_pred)
    setosa=np.array([y_pred[i][0] for i in range(len(y_pred))])
    versicolor=np.array([y_pred[i][1] for i in range(len(y_pred))])
    virginica=np.array([y_pred[i][2] for i in range(len(y_pred))])
    
    df2=pd.DataFrame({'setosa': [len(setosa[setosa>.5])],'versicolor': 
    [len(versicolor[versicolor>.5])], 'virginica': [len(virginica[virginica>.5])] })
    df2['id']=np.arange(0,1)
    df2=df2.set_index('id')
    df2.plot.bar()
    plt.show()
    
    0 讨论(0)
提交回复
热议问题