Python Forum

Full Version: copy columns without header name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how to copy columns from df to new columns without knowing the columns header name?

eg below is with the known header name
df["new"]= df["old"]
how about without known the header name?
df["new"] = df[0] this does not work
You should be able to refer to the dataframes column by its index position as:
df.columns[1]
Assuming that the 'old' column is at position 1

Thus I think you can go:

df['new'] = df.columns[1]