0

I have a dataframe column names which are in form of string I would like to convert it into tuple, which shows the data inside of those column names. I have used "eval" but since its dangerous to use is there any other way to convert?

input: dt = "df['cars'],df['bikes']"
output: dt = (df['cars'],df['bikes'])
4
  • What do you want to be printed when you run print(dt)? You cannot get (df['cars'],df['bikes']) if this is what you want. It will print the entire column instead Commented Jan 11, 2021 at 8:13
  • So, basically I am dealing with flask and html. When a user sends a input(say the columns names) it will be received as a string, I want them to be converted into tuple for doing further operation. Commented Jan 11, 2021 at 8:31
  • This is a different question. Amend your question or create a new one (delete this one) providing your relevant flask and html code Commented Jan 11, 2021 at 8:34
  • please check the below link stackoverflow.com/questions/65678191/… Commented Jan 12, 2021 at 5:44

1 Answer 1

1

use spit(',') method

dt = "df['cars'],df['bikes']"
tu = tuple(dt.split(','))
print(tu)

output: ("df['cars']", "df['bikes']")

Sign up to request clarification or add additional context in comments.

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.