Issue
I am using html
and flask
. If a user enters a value with key in text box, how do I convert that into a dictionary?
Input :
'x':df['region'], 'y':df['age'], 'z': df['smoker']
Output:
{'x':df['region'], 'y':df['age'], 'z': df['smoker']}
Solution
If you have it as string
query = "'x':df['region'], 'y':df['age'], 'z': df['smoker']"
then you could use eval()
to convert it
data = eval("{" + query + "}")
Maybe it is not safe but it is fast and easy.
import pandas as pd
df = pd.DataFrame({
'region': [1,2,3],
'age': [4,5,6],
'smoker': [7,8,9],
'other': ['A','B','C'],
})
query = "'x':df['region'], 'y':df['age'], 'z': df['smoker']"
data = eval("{" + query + "}")
print(data)
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.