【问题标题】:Start index (under FIeld) from 1 with pandas DataFrame使用 pandas DataFrame 从 1 开始索引(在 FIeld 下)
【发布时间】:2021-09-13 01:19:35
【问题描述】:

我想在“字段”列中从 1 开始索引

df = pd.DataFrame(list(zip(total_points, passing_percentage)),
                 columns =['Pts Measured', '% pass'])
df = df.rename_axis('Field').reset_index()
df["Comments"] = ""
df

输出:

  Field  Pts Measured   % pass  Comments
0   0       92909       90.66   
1   1       92830       91.85   
2   2      130714       99.99   

【问题讨论】:

    标签: python pandas dataframe indexing


    【解决方案1】:

    我在这里发现了一个类似的问题:In Python pandas, start row index from 1 instead of zero without creating additional column

    对于您的问题,只需添加以下行:

    df["Field"] = np.arange(1, len(df) + 1)
    

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 2013-01-01
      • 2016-12-27
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多