Aizzaac
Aizzaac

Reputation: 3318

How to change the name of columns of a Pandas dataframe when it was saved with "pickle"?

I saved a Pandas DataFrame with "pickle". When I call it it looks like Figure A (that is alright). But when I want to change the name of the columns it looks like Figure B. What am I doing wrong? What are the other ways to change the name of columns?

Figure A

enter image description here

Figure B

enter image description here

import pandas as pd

df = pd.read_pickle('/home/myfile')
df = pd.DataFrame(df, columns=('AWA', 'REM', 'S1', 'S2', 'SWS', 'ALL')) 
df

Upvotes: 0

Views: 1704

Answers (2)

schlump
schlump

Reputation: 1191

read.pickle already returns a DataFrame. And you're trying to create a DataFrame from an existing DataFrame, just with renamed columns. That's not necessary...

As you want to rename all columns:

df.columns = ['AWA', 'REM','S1','S2','SWS','ALL']

Renaming specific columns in general could be achieved with:

df.rename(columns={'REM':'NewColumnName'},inplace=True)

Pandas docs

Upvotes: 2

Aizzaac
Aizzaac

Reputation: 3318

I have just solved it.

df = pd.read_pickle('/home/myfile')

df1 = pd.DataFrame(df.values*100)
df1.index='Feature' + (df1.index+1).astype(str) 
df1.columns=('AWA', 'REM', 'S1', 'S2', 'SWS', 'ALL')

df1

Upvotes: 0

Related Questions