Home > database >  How to map column values to a formatted string (% formatting) in another pandas' column?
How to map column values to a formatted string (% formatting) in another pandas' column?

Time:10-17

I have a pandas dataframe (df) with a df['Message'], df['value1'], df['value2'] ...etc The Messages in the ['Message'] column are formatted strings such as: 'CPU-Value: %d, Event-Type: %s, ..,..'

My goal is to replace the %d or %s in ['Message'] with values from the other columns ['value1'] and ['value2']...

The following approach would not work: (it works only for {} formatted strings)

for index, row in df['Message'].items():
     df['Message'] = df['Message'][index].format(str(df['value1'][index]), ......)

Is there any advice on how to replace the variables/format the string with values from other columns.

CodePudding user response:

Since your variables in df['Message'] should be all formatted the same way (same order of %d, %s, etc..), you can just use a str.replace(str1,str2,occurrencies):

df = pd.DataFrame({'mes':[ 'CPU-Value: %d, Event-Type: %s'],'d':[1],'s':['a']})

def formatter(a,b,c):
    a = a.replace('%d',str(b),1)
    a = a.replace('%s',str(c),1)
    return a

df['formatted'] = df.apply(lambda x: formatter(x['mes'],x['d'],x['s']), axis=1)

CodePudding user response:

You can apply a function (e.g. format) to an axis (e.g. per row via axis=1) with df.apply:

import pandas as pd

#sample data
df = pd.DataFrame.from_dict({
    'value1': [0, 1, 2],
    'value2': ['TestA', 'TestB', 'TestC']
})

#format message with values
df['message'] = df.apply(lambda x: f"CPU-Value: {x['value1']:d}, Event-Type: {x['value2']}", axis=1)
  • Related