使用 DataFrame 我想添加一个将写入 Field1、Field2 的列...(包含与索引 1 一样多的行)

Using DataFrame I want to add a column that will write Field1, Field2...(containing as many rows as the index-1)

我想使用 DataFrame 添加一个列来写入 Field1、Field2...(包含与索引 1 一样多的行)。

import numpy as np
df = pd.DataFrame(list(zip(total_points, passing_percentage)),
                 columns =['Pts_Measured', '%_pass'])
df = df.rename_axis('Field').reset_index()
df["Comments"] = ""
df["Field"] = np.arange(1, len(df) + 1)
df

输出:

Field   Pts_Measured    %_pass  Comments
    0   1          92909         90.66  
    1   2          92830         91.85  
    2   3         130714         99.99  

这就是我想要的:

  Field     Field_num       Pts_Measured     %_pass   Comments
0   1          Field1            92909       90.66  
1   2          Field2            92830       91.85  
2   3          FIeld3           130714       99.99  
.. ....   ............      ..........       .......   

希望我已经正确理解您的问题。如果你有这个数据框:

   Pts_Measured  %_pass  Comments
0             1   92909     90.66
1             2   92830     91.85
2             3  130714     99.99

那么你可以这样做:

df["Field"] = np.arange(len(df)) + 1
df["Field_num"] = "Field" + df["Field"].astype(str)

print(df)

打印:

   Pts_Measured  %_pass  Comments  Field Field_num
0             1   92909     90.66      1    Field1
1             2   92830     91.85      2    Field2
2             3  130714     99.99      3    Field3