How to map a column in data frame using pyhton

69 views Asked by At

I need to map a column names in data frame using python, I have some different data set in my csv need to match (mapping) a columns to standard name like following .

set 1 set 2

userId :[(1,2,3)] customerId : [(1,2,3)] userName :[('sam','ram','mam')] customerName : [('raj','tej','tej')] contact : [('sam@gmail','ram@gmail','mam@gmail')] email : [('raj@gmail','tej@gmail','tej@gmail')]

I need like

pd[id]=pd[userId] or pd[customerId] pd[name]=pd[userName ] or pd[customerName]

I have tried or condition using pandas . its working but I needd some standard solution.

if 'Number' in df.columns:
    df_new = df.rename(columns = {'Number': 'Id'})
    if 'Address' in df.columns:
    df_new = df.rename(columns = {'Address': 'address'})

1

There are 1 answers

0
ziying35 On

try this:

mapper = {'Number': 'Id', 'Address': 'address'}
# If 'ignore', existing keys will be renamed and extra keys will be ignored.
df.rename(columns=mapper, errors='ignore')