Home > Back-end >  Function to replace values in columns with Column Headers (Pandas)
Function to replace values in columns with Column Headers (Pandas)

Time:01-10

I am trying to create a function that loops through specific columns in a dataframe and replaces the values with the column names. I have tried the below but it does not change the values in the columns.


def value_replacer(df):
    cols = ['Account Name', 'Account Number', 'Maintenance Contract']
    x= [i for i in df.columns if i not in cols]
    
    for i in x:
        for j in df[i]:
            if isinstance(j,str):
                j.replace(j,i)
    return df

What should be added to the function to change the values?

CodePudding user response:

Similar to @lazy's solution, but using difference to get the unlisted columns and using a mask instead of the list comprehension:

df = pd.DataFrame({'w': ['a', 'b', 'c'], 'x': ['d', 'e', 'f'], 'y': [1, 2, '3'], 'z': [4, 5, 6]})
def value_replacer(df):
  cols_to_skip = ['w', 'z']
  for col in df.columns.difference(cols_to_skip):
    mask = df[col].map(lambda x: isinstance(x, str))
    df.loc[mask, col] = col
  return df

Output:

Output dataframe

Loop through only the columns of interest once, and only evaluate each row within each column to see if it is a string or not, then use the resulting mask to bulk update all strings with the column name. Note that this will change the dataframe inplace, so make a copy if you want the original, and you don't necessarily need the return statement.

CodePudding user response:

You can change it this way

def value_replacer(df):
    cols = ['Account Name', 'Account Number', 'Maintenance Contract']
    x= [i for i in df.columns if i not in cols]
    
    for i in x:
        for idx,j in enumerate(df[i]):
            if isinstance(j,str):
                df.loc[idx,j] = i
    return df

or

def value_replacer(df):
    cols = ['Account Name', 'Account Number', 'Maintenance Contract']
    x= [i for i in df.columns if i not in cols]
    
    for i in x:
        df[i] = [i if isinstance(j,str) else j for j in enumerate(df[i])]
    return df
  •  Tags:  
  • Related