0

I need to derive a new column from existing column in dataframe. this logic will be different for different runs. For this, I have decided to have a config file so that logic can be updated in config file easily.

logic_Entry in config file:

df['col1'].str[1:4]

used in code like below:

df['col2']= logic_Entry

but when I check the output in col2 it just puts the logic as a literal, so in this example I get value as df['col1'].str[1:4] for all records.

I was expecting it to instead evaluate this logic and put substring of col1 in this new column (col2).

Can someone please suggest what am I missing?

Barmar
  • 741,623
  • 53
  • 500
  • 612
PythonDeveloper
  • 289
  • 1
  • 4
  • 24
  • *I was expecting it to instead evaluate this logic* : You are probably looking for `eval()`. This executes whatever python code you put inside a string. But you should not do this approach. You have an XY problem with how you are using your config file. See [this question](https://stackoverflow.com/q/701802/22390653) for context. – PatioFurnitureIsCool Aug 21 '23 at 19:22
  • Do you really need to be able to put arbitrary logic in the config file? Or could you use something like `col=col1` and `substring=1:4` – Barmar Aug 21 '23 at 19:23

0 Answers0