Home > Enterprise >  How to append a column from back into a DataFrame?
How to append a column from back into a DataFrame?

Time:11-24

As in Given DataFrame redshift is in middle I want redshift column as the last column of DataFrame

I tried this code

x=dataset.drop('redshift',axis=1)
y=dataset['redshift']
x['redshift']=y
dataset=x

now after executing this code dataset is having redshift as last column but when Saving this dataframe into csv file and Loading again that csv file I am getting that redshift as last column.

CodePudding user response:

IIUC, try:

y = dataset['redshift']              # <- swap this line
x = dataset.drop('redshift',axis=1)  # <- and this line
x['redshift'] = y
dataset['redshift'] = x              # <- set column name

CodePudding user response:

I think you are trying to pop of a column from a dataframe.

y = dataset.pop('redshift')
X = dataset

Now why is a pd.Series of 'redshift' and X is the original dataset without 'redshift'.

  • Related