0

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
Aizzaac
  • 3,146
  • 8
  • 29
  • 61

2 Answers2

2

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

schlump
  • 1,131
  • 1
  • 9
  • 14
  • And then, how can I do the multiplication of each value by 100, without creating a new DataFrame? – Aizzaac Nov 01 '16 at 15:08
0

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
Aizzaac
  • 3,146
  • 8
  • 29
  • 61