Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i return a dataframe from a function

i have a function that creates a dataframe. This is the first function. In the second function i want to use the dataframe that i have created in the first function.

That is the shorten function:

def einlesen():
    rbu = pd.read_excel('INPUT\RBU_COIN2.xlsx') #RBU-DATEN AUS EXCEL EINLESEN
    data_faktura = pd.merge(rbu, hvl, on='EQ_NR', how='left') #RBU UND HVL ANHAND DER EQ_NR ZUGEORDNET
    print('RBU-DATEN UND HVL-DATEN ZUSAMMENGEFASST!')
    data_faktura['TA'] = data_faktura['TA'].replace(['1', '2', 'a ', '3', '4', '5', '6'], '7') #TA NACH SUCHKRITERIEN ERSETZT
    #data_faktura.to_excel('fakt_daten.xlsx', index=False) #OUTPUT ALS EXCELDATEI
    print('Daten für Berechnung des Zwischenergebnisses erfasst!')
    Button_start1.config(state=ACTIVE) #BUTTON WIEDER AUF AKTIV GESETZT
    msg_oben.config(text='Daten eingelesen! Bitte Berechnung starten!')
    #os.startfile('fakt_daten.xlsx')
    return data_faktura

as you can see i tried to return the dataframe from the function with "return data_faktura" but it didnt work.

i try to call the dataframe "data_faktura" like that:

def zwischenergebnis(data_faktura):
    data_kl2m = data_faktura # EINLESEN VON TA '<2M' UND BILDUNG DER SUMME
    data_kl2m = data_kl2m[data_kl2m.TA == '<2M']

Here is the error i get:

TypeError: zwischenergebnis() missing 1 required positional argument: 'data_faktura'

Thats how i call the functions:

Button_einlesen = ttk.Button(mainWin, text='Faktura einlesen!', command=einlesen)
Button_einlesen.grid(row=4, columnspan=6, sticky="ew")
Button_start1= ttk.Button(mainWin, text='Zwischenergebnisse berechnen!', state=DISABLED, command=zwischenergebnis)
Button_start1.grid(row=5, column=0, columnspan=3, sticky="ew")

How can i solve that? Or what im doing wrong here?

Thank you for your help!


@tzaman:

here is the error i get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 93, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "C:\Users\---\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 250, in nansum
    the_sum = values.sum(axis, dtype=dtype_sum)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\numpy\core\_methods.py", line 32, in _sum
    return umr_sum(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for +: 'float' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\----\Winpython\python-3.4.3\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\----\[INPROGRESS] Faktura_sylvia\Faktool\geruest_tool.py", line 221, in zwischenergebnis
    data_ags1_mw_KW = data_ags1_mw['KW_WERT'].sum()
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\generic.py", line 4255, in stat_func
    skipna=skipna, numeric_only=numeric_only)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\pandas\core\series.py", line 2084, in _reduce
    return op(delegate, skipna=skipna, **kwds)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 45, in _f
    return f(*args, **kwargs)
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 95, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)
  File "C:\Users\----\Winpython\python-3.4.3\lib\site-packages\pandas\core\nanops.py", line 250, in nansum
    the_sum = values.sum(axis, dtype=dtype_sum)
  File "C:\Users\-----\Winpython\python-3.4.3\lib\site-packages\numpy\core\_methods.py", line 32, in _sum
    return umr_sum(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
like image 755
Damian Avatar asked Feb 03 '26 01:02

Damian


1 Answers

Your button callbacks can't communicate directly via returning and passing parameters, since you're not actually calling them yourself.

Instead, you could do something like adding them both to a class and using an instance variable. Something like this:

class Commands():
    def einlesen(self):
        # ... all your code
        self.df = data_faktura

    def zwischenergebnis(self):
        data_faktura = self.df
        # proceed as before

Then you can instantiate a Commands object and bind its methods as your button callbacks:

commander = Commands()
Button_einlesen = ttk.Button(mainWin, text='..', command=commander.einlesen)
Button_start1= ttk.Button(mainWin, text='..', command=commander.zwischenergebnis)
like image 85
tzaman Avatar answered Feb 12 '26 22:02

tzaman