0

Suppose we have a string and a dataframe like this:

import pandas as pd

myStr="Insert {var1} here & here {var1} and insert {var2} here & here {var2}."
df = pd.DataFrame({'Var1': (1, 2), 'Var2': ('B', 'C')}

>>> df
   Var1 Var2
0     1    B
1     2    C

Now I add the myStr as a new column and I wanna insert the the var 1 and var2 their locations within the myStr. I am looking to get something like below. I was thinking to use f-strings but not sure what is the best way to do it in pandas.

Var1 Var2    new_col
1    B       Insert 1 here & here 1 and insert B here & here B.
2    C       Insert 2 here & here 2 and insert C here & here C.

4 Answers 4

2

This is one way about it; create a list comprehension and generate a new dataframe - it should be easier and faster than using apply :

outcome = [(var1,var2,
            f"Insert {var1} here & here {var1} and insert {var2} here & here {var2}.")
            for (var1, var2) in df.to_numpy()]

pd.DataFrame(outcome,columns=["Var1","Var2","new_col"])

    Var1    Var2    new_col
0   1   B   Insert 1 here & here 1 and insert B here & her...
1   2   C   Insert 2 here & here 2 and insert C here & her...

Alternatively, using the myStr variable, in combination with the format method :

outcome = [(var1,var2, myStr.format(var1=var1,var2=var2))
            for (var1, var2) in df.to_numpy()]

Let's see the contents of outcome :

print(outcome)

[(1, 'B', 'Insert 1 here & here 1 and insert B here & here B.'),
 (2, 'C', 'Insert 2 here & here 2 and insert C here & here C.')]

Create dataframe, similar to the method above:

 pd.DataFrame(outcome,columns=["Var1","Var2","new_col"])
Sign up to request clarification or add additional context in comments.

Comments

2

Use apply and axis parameter:

df['new_col'] = df.apply(lambda x: myStr.format(var1=x['Var1'], var2=x['Var2']), axis=1)

The axis parameter iterates over rows.

Comments

1

Another one-liner:

df['new_col']=[f'Insert {i[0]} here & here {i[0]} and insert {i[1]} here & here {i[1]}.' for i in df.values]

Comments

0

Probably not the fastest way, but you didn't say you were working with anything big.

Axis=1 makes the apply go row by row.

df.apply((lambda row: row['new_col'].format(var1=row['Var1'], var2=row['Var2'])), axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.