df.apply
is very popular but should be avoided whenever possible.
Instead use vectorized methods. Convert the relevant columns to dict and remove the quotes:
df["prompt"] = df[["foo", "bar"]].to_dict(orient="index")
df["prompt"] = df["prompt"].astype(str).replace(r"'", "", regex=True)
# foo bar new prompt
# 0 a 1 apple {foo: a, bar: 1}
# 1 b 2 banana {foo: b, bar: 2}
# 2 c 3 pear {foo: c, bar: 3}
Note that your comment included braces but your post did not. If you also want to remove the curly braces, add them to the regex:
df["prompt"] = df["prompt"].astype(str).replace(r"[{'}]", "", regex=True)
# foo bar new prompt
# 0 a 1 apple foo: a, bar: 1
# 1 b 2 banana foo: b, bar: 2
# 2 c 3 pear foo: c, bar: 3
Details
First convert the relevant columns to_dict
oriented by index:
df["prompt"] = df[["foo", "bar"]].to_dict(orient="index")
# foo bar new prompt
# 0 a 1 apple {'foo': 'a', 'bar': 1}
# 1 b 2 banana {'foo': 'b', 'bar': 2}
# 2 c 3 pear {'foo': 'c', 'bar': 3}
Then use astype
to convert it to str
type and replace
the dict symbols:
df["prompt"] = df["prompt"].astype(str).replace(r"[{'}]", "", regex=True)
# foo bar new prompt
# 0 a 1 apple foo: a, bar: 1
# 1 b 2 banana foo: b, bar: 2
# 2 c 3 pear foo: c, bar: 3