How to add new column to existing dataframe (no headers) using iloc?

Viewed 29

I want to create third column to existing dataframe, having the same values of 2nd column using iloc pandas method. What are the options do I have ?

df = pd.DataFrame([*zip([1,2,3],[4,5,6])])
1 Answers

here is one way to do it

df.insert(len(df.columns), # position of new column, it points to column after last column
          len(df.columns), # naming could be the location of column
          value=df.iloc[:,1] # grab the values from last column
         )
df

    0   1   2
0   1   4   4
1   2   5   5
2   3   6   6
Related