Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change column names of Pandas Series object?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

             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..

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading