0

I am having troubles converting the column names in my pandas dataframe according to the dictionary I have created

housing = pd.read_csv('City_Zhvi_AllHomes.csv')
cols = housing.iloc[:,51:251]
housing = housing.drop(list(housing)[6:251],axis=1)
cols = cols.groupby(np.arange(len(cols.columns))//3, axis=1).mean()
a= pd.read_excel('gdplev.xls', header=None, skiprows=220,index_col=0, names=['GDP'], parse_cols=[4,6])
col_names = list(a.index)
col_names = col_names + ['2016q3']
vals = list(cols.columns.values)
cols_dict = dict(zip(col_names,vals))
cols = cols.rename(columns = cols_dict)

I also tried using the map function:

cols.columns.map([cols_dict])

The desired outcome is to convert all the column names (0-66) to they keys listed in my dictionary (2000q1-2016q3)

However, the two solutions I have implemented yield the same results and the columns remain with the same names.

UPDATE As requested here is a list of the first few rows from my dataframe:

0   1   2   3   4   5   6   7   8   9   ... 57  58  59  60  61  62  63  64  65  66
0   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 5.154667e+05    5.228000e+05    5.280667e+05    5.322667e+05    5.408000e+05    5.572000e+05    5.728333e+05    5.828667e+05    5.916333e+05    587200.0
1   2.070667e+05    2.144667e+05    2.209667e+05    2.261667e+05    2.330000e+05    2.391000e+05    2.450667e+05    2.530333e+05    2.619667e+05    2.727000e+05    ... 4.980333e+05    5.090667e+05    5.188667e+05    5.288000e+05    5.381667e+05    5.472667e+05    5.577333e+05    5.660333e+05    5.774667e+05    584050.0
2   1.384000e+05    1.436333e+05    1.478667e+05    1.521333e+05    1.569333e+05    1.618000e+05    1.664000e+05    1.704333e+05    1.755000e+05    1.775667e+05    ... 1.926333e+05    1.957667e+05    2.012667e+05    2.010667e+05    2.060333e+05    2.083000e+05    2.079000e+05    2.060667e+05    2.082000e+05    212000.0
3   5.300000e+04    5.363333e+04    5.413333e+04    5.470000e+04    5.533333e+04    5.553333e+04    5.626667e+04    5.753333e+04    5.913333e+04    6.073333e+04    ... 1.137333e+05    1.153000e+05    1.156667e+05    1.162000e+05    1.179667e+05    1.212333e+05    1.222000e+05    1.234333e+05    1.269333e+05    128700.0

And a sample of my dictionary:

{0: '2000q1',
 1: '2000q2',
 2: '2000q3',
 3: '2000q4',
 4: '2001q1',
 5: '2001q2',
Emm
  • 2,367
  • 3
  • 24
  • 50

1 Answers1

0

you can rename columns in this way:

#Rename Columns
df.rename(columns={'old name1':'new name1','old name2':'new name2'}, inplace=True)

so you'd just use:

housing.rename(columns=cols_dict, inplace=True)

But change you dictionary where the keys are the old column names, and the value of those keys are the new names

cols_dict = dict(zip(vals, col_names))

Looking at your code though, it looks like you’re applying that to a grouby Object. So you’d need to change that ‘cols’ Object back to just simple normal dataframe, then do your rename, or they explain how to do it here with that groupby function Renaming Column Names in Pandas Groupby function

chitown88
  • 27,527
  • 4
  • 30
  • 59
  • have tried this, returns cols(housing) as a NoneType – Emm Dec 13 '18 at 06:44
  • can you provide the first few rows of your dataframe and also provide your dictionary (compact...only first 3-5 columns. i'm assuming you have 67 columns) – chitown88 Dec 13 '18 at 06:49
  • And also, did you change your dict? As stated in my answer? You’ve shown 2 different sample of your dictionary, but the line to get that dictionary hasn’t changed. – chitown88 Dec 13 '18 at 07:16
  • Yes, solution worked eventually- will accept your answer...thanks. I was savings cols.rename to the cols variable then calling cols, which returned the NoneType. I didn't need to save cols.rename in the cols variable. – Emm Dec 13 '18 at 07:28