Python Forum
copy columns without header name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
copy columns without header name
#1
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
Reply
#2
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 257 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,161 May-03-2022, 05:46 PM
Last Post: ahmedbarbary

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020