How to change column names of Pandas Series object?

Viewed 84

I'm trying to prefix the names of the columns for each series in my Pandas Series, based on one of the other columns value. Currently my objective is to change a Pandas Dataframe that contains 3 columns into a Dataframe of only 1 column named 'Data' - or whatever. Below is an example of stacking a Dataframe to obtain a single dimension to work with.

df_single_level_cols = pd.DataFrame([[0, 1, 20], [2, 3, 40]],columns=['weight', 'height', 'girth'])
df = df_single_level_cols.stack()
print(df)
0  weight     0
   height     1
   girth     20
1  weight     2
   height     3
   girth     40
dtype: int64

For each series I need to prefix both column names weight and height with the value of girth. When that is done I will drop girth from the equation, leaving me with only the weight and height for my series. After prefixing and dropping the series object should look like the following:

0  20weight     0
   20height     1
1  40weight     2
   40height     3
dtype: int64

Then when converting this to a Dataframe I shall have the following:

             Data
20weight     0
20height     1
40weight     2
40height     3

I've tried messing around with .apply(...), .rename(...) and .add_prefix(...) but none of them seem to be doing the trick. If I do something like

df[0] = df[0].add_prefix("test")

I end up getting errors as I'm setting an array element with a sequence + this does not actually use the value of girth - but was more a way of getting accustomed with the rename functionality..

2 Answers

You can melt instead:

df = (df_single_level_cols
 .astype({'girth': str})
 .melt('girth', value_name='Data')
 .assign(**{'girth': lambda d: d['girth']+d.pop('variable')})
 .set_index('girth')
)

output:

          Data
girth         
20weight     0
40weight     2
20height     1
40height     3

Like this?

df = pd.DataFrame([[0, 1, 20], [2, 3, 40]],columns=['weight', 'height', 'girth'])
df = df[['weight', 'height']].stack().reset_index(level=1).merge(df.girth, left_index=True, right_index=True, how='left')
df = df.set_index(df.girth.astype(str) + df.level_1).rename(columns={0: 'Data'})[['Data']]

> df

    Data
20weight    0
20height    1
40weight    2
40height    3
Related