How to name columns in pandas

Code examples

5
0

create dataframe with column names pandas

In [4]: import pandas as pd
In [5]: df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
In [6]: df
Out[6]:
Empty DataFrame
Columns: [A, B, C, D, E, F, G]
Index: []
1
0

how to change column name in pandas

print(df.rename(columns={'A': 'a', 'C': 'c'}))
#         a   B   c
# ONE    11  12  13
# TWO    21  22  23
# THREE  31  32  33
1
0

name of columns pandas

df.columns
1
0

how to give name to column in pandas

>gapminder.rename(columns={'pop':'population',
                          'lifeExp':'life_exp',
                          'gdpPercap':'gdp_per_cap'}, 
                 inplace=True)
 
>print(gapminder.columns)
 
Index([u'country', u'year', u'population', u'continent', u'life_exp',
       u'gdp_per_cap'],
      dtype='object')
 
>gapminder.head(3)
 
       country  year  population continent  life_exp  gdp_per_cap
0  Afghanistan  1952     8425333      Asia    28.801   779.445314
1  Afghanistan  1957     9240934      Asia    30.332   820.853030
2  Afghanistan  1962    10267083      Asia    31.997   853.100710
1
0

change name of column pandas

#df.rename() will only return a new df with the new headers
#df = df.rename() will change the heders of the current dataframe 
df = df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"})
0
0

name columns pandas

>gapminder.columns = ['country','year','population',
                     'continent','life_exp','gdp_per_cap']

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................