0

i'm passing a function who needs a dataframe to execute it, inside a variable string, and using the eval metric i'm trying to execute the function. But the code isn't working because the dataframe becames a string and it fails to understend it.

What i'm doing is:

def function(dataframe, number1, number2):
    print(dataframe) #I need this print(dataframe) to print the dtaframe in the correct dataframe format
    print(number1+number2)

dataframe = pandas.DataFrame()
number1 = 1
number2 = 1
a = f'function{dataframe, number1, number2}'
eval(a)

But this do'nt work

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
yyz_vanvlet
  • 171
  • 2
  • 8

1 Answers1

0

You can just call the function and it will work:

from IPython.display import display, HTML
import pandas

def function(dataframe, number1, number2):
    display(HTML(dataframe.to_html()))
    print(number1+number2)

dataframe = pandas.DataFrame()
number1 = 1
number2 = 1
a = f'function(dataframe, number1, number2)'
eval(a)

If you want to display the datafram in good formatting I suggest reading here: Show DataFrame as table in iPython Notebook

Bruno Mello
  • 4,448
  • 1
  • 9
  • 39