Reputation: 3895
I want to rename one column in pandas dataframe. I want to do that by using apply function. I wrote a code that does that but I do not know how to use apply fucntion to do this . Can anybody help?
import pandas as pd
import numpy as np
import datetime
url = 'https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv'
df_github = pd.read_csv(url)
df_github = df_github.rename(columns={'name':'Country'})
Upvotes: 0
Views: 1384
Reputation: 481
I would agree with jezrael, this just makes it unnecessarily complicated. That's how I would do it as a 'quick and dirty' solution in case you really want to use apply
(obviously very similar to jezrael's solution):
df_github.columns = df_github.columns.to_series().apply(lambda x: 'Country' if x == 'name' else x )
Upvotes: 1
Reputation: 863751
It is possible, but overcomplicated, because need convert index to series and then call lambda
function with apply
:
d = {'name':'Country'}
df_github.columns = df_github.columns.to_series().apply(lambda x: d.get(x, x))
Upvotes: 1