How to add a column to a Pandas DataFrame

0 min

You will often need to add an extra column to a DataFrame.

You can easily do this like so

# Here we assign the range values 
# to the new column col2
df["col2"] = range(0,len(df))
# Here we base the second column and add 1
df["col2"] = df["col1"] + 1
# Here we base the second column and add 1 using lambda functions
df["col2"] = df["col2"].apply(lambda x: x + 1)