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

Viewed 30

Using DataFrame I want to add a column that will write Field1, Field2...(containing as many rows as the index-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

Output:

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

This is what I would want to have:

  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  
.. ....   ............      ..........       .......   
1 Answers

I hope I've understood your question right. If you have this dataframe:

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

then you can do:

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

print(df)

prints:

   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
Related