pandas add column names to dataframe code example
Example 1: how to add column headers in pandas
team.columns =['Name', 'Code', 'Age', 'Weight']
Example 2: how to add headings to data in pandas
Cov = pd.read_csv("path/to/file.txt", sep='\t')
Frame=pd.DataFrame(Cov.values, columns = ["Sequence", "Start", "End", "Coverage"])
Frame.to_csv("path/to/file.txt", sep='\t')
Example 3: how to give column names in pandas when creating dataframe
df_new = df.rename(columns={'A': 'a'}, index={'ONE': 'one'})
print(df_new)
print(df)
Example 4: 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
Example 5: name columns pandas
>gapminder.columns = ['country','year','population',
'continent','life_exp','gdp_per_cap']