python dataframe rename

# -*- coding: UTF-8 -*-
import pandas as pd
df = pd.DataFrame([{'col1':'a', 'col2':1, 'col3':'aa'}, {'col1':'b', 'col2':2, 'col3':'bb'}, {'col1':'c', 'col2':3, 'col3':'cc'}, {'col1':'a', 'col2':44, 'col3':'aa'}])
data = df.groupby(by='col1').agg({'col2':['max', 'min']}).reset_index()

print data
data.columns = [' '.join(col).strip()for col in data.columns.values]
yy = data.rename(columns={'col2 max':'max_col2', 'col2 min':'min_col2'})
print yy

输出结果:

  col1 col2    
        max min
0    a   44   1
1    b    2   2
2    c    3   3
  col1  max_col2  min_col2
0    a        44         1
1    b         2         2
2    c         3         3

你可能感兴趣的:(python)