I'm trying to use my custom function within pandas eval. It works properly for limited use:
basic_df = DataFrame({"A":[1,2,3,4,5],"B":[20,40,60,100,90],
"C":["C1","C2","C3","C4","C5"],
})
def str_parse(element) -> str:
return str(element)
print(basic_df.eval("@str_parse(A+B+100)"))
But whenever I want to add some static string (to add string to string), it returns following result:
basic_df.eval("@str_parse(A+B+100) + \"additional string\"",)
0 121
1 142
2 163
3 204
4 195
dtype: int64additional string.
How can i add string to string within creating additional column?
First, return a new series with string type from
str_parse()(not just string representation of Series). Then you can use.__add__()to add additional string (for some reason, simple+doesn't work):Prints: