Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename columns dynamically before unstack in pandas?

Tags:

python

pandas

I have the below dataframe created using groupby and sum :-

year_month  Country           
2008-01     Afghanistan             2
            Albania                 3
            Argentina               4
2008-02     Afghanistan             3
            Albania                 4
            Argentina               5

I need to unstack and want name to be renamed as der_value_Afghanistan, der_value_Albania etc as column names rather than Afghanistan etc. Since it could be 100 or more, is there any way to rename it all together rather than manually?

year_month der_value_Afghanistan der_value_Albania der_value_Argentina
like image 453
user3222101 Avatar asked Dec 18 '22 21:12

user3222101


1 Answers

I think need Series.unstack with DataFrame.add_prefix:

df = s.unstack().add_prefix('der_value_')
print (df)
Country     der_value_Afghanistan  der_value_Albania  der_value_Argentina
year_month                                                               
2008-01                         2                  3                    4
2008-02                         3                  4                    5

For index to column add DataFrame.rename_axis with DataFrame.reset_index:

df = s.unstack().add_prefix('der_value_').rename_axis(None, axis=1).reset_index()
print (df)
  year_month  der_value_Afghanistan  der_value_Albania  der_value_Argentina
0    2008-01                      2                  3                    4
1    2008-02                      3                  4                    5

Modify MultiInex before unstack is also possible by MultiIndex.from_arrays:

a = s.index.get_level_values(0)
b = 'der_value_' + s.index.get_level_values(1)
s.index = pd.MultiIndex.from_arrays([a, b], names=s.index.names)
print (s)
year_month  Country              
2008-01     der_value_Afghanistan    2
            der_value_Albania        3
            der_value_Argentina      4
2008-02     der_value_Afghanistan    3
            der_value_Albania        4
            der_value_Argentina      5
Name: a, dtype: int64

df = s.unstack()
print (df)
Country     der_value_Afghanistan  der_value_Albania  der_value_Argentina
year_month                                                               
2008-01                         2                  3                    4
2008-02                         3                  4                    5
like image 187
jezrael Avatar answered Mar 03 '23 15:03

jezrael