0

I have a faw data file and I need to split the column index 0 by comma. The dataset has no headers so I had to control index by number. After split, I will need to hash one of the column values before joining them back.

I using the code below to split.

Test = pd.concat([df,df[0].str.split(',',expand=True)],axis=1)

The column did split successfully but it appended the new columns after the last (which is 29 for my case) as 0,1,2,3,4, etc. Now I have repeated column numbers which I csnt rename properly. If I rename column 0 to 30, 2 columns will be rename.

I have tried using Test.reindex(axis=1) but nothing happens to the column index number.

How do indexed index column correctly?

user3782604
  • 330
  • 1
  • 19
  • 1
    kindly provide data, with expected output. [guide](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – sammywemmy Apr 06 '20 at 08:32

1 Answers1

1

You can rename new columns, e.g. by DataFrame.add_prefix:

Test = pd.concat([df,df[0].str.split(',',expand=True).add_prefix('new')],axis=1)

Or for default columns names by range use:

Test = pd.concat([df,df[0].str.split(',',expand=True)],axis=1)
Test.columns = np.arange(len(Test.columns))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252