Home > Net >  How to map a column in data frame using pyhton
How to map a column in data frame using pyhton

Time:12-06

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'})

CodePudding user response:

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')

CodePudding user response:

You could try something like:

from typing import Hashable, Optional, Dict
import pandas as pd

def rename_columns(
    df: pd.DataFrame,
    rename_dict: Optional[Dict[str, str]] = None,
) -> pd.DataFrame:
    """Rename columns in a dataframe, using pre-mapped column names.

    Parameters
    ----------
    df : pd.DataFrame
        A pandas dataframe to rename columns.
    rename_dict : Optional[Dict[str, str]]
        Extra patterns to use for renaming columns.

    Returns
    -------
    pd.DataFrame
        A pandas dataframe with renamed columns.
    """
    rename_dict = {} if rename_dict is None else rename_dict

    df = df.rename(
        columns={
            col: (col.lower().replace(' ', '_').replace('-', '') if isinstance(
                col, str) else col)
            for col in df.columns
        }
    )
    rename_dict = {
        **{
            col: 'id' for col in [
                col for col in df.columns
                if col == 'number' or 'id' in col
            ]
        },
        **{
            col: 'name' for col in [
                col for col in df.columns if 'name' in col
            ]
        },
        **{
            col: 'contact' for col in [
                col for col in df.columns if 'email' in col
            ]
        },
        **rename_dict,
    }
    return df.rename(columns=rename_dict, errors='ignore')


df = rename_columns(df)

  • Related