How to use the output of a function for a second function

Question:

I have a function that creates and prints a Dataframe based on the contents of a file:

import pandas as pd

def DataFrameConverter(file):

    T = pd.read_fwf(file, header= None, names=['1','2','3','4','5','6','7','8','9'])
    T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})

    df = pd.DataFrame(T)
    print(df)

and a function that is supposed to allow the user to modify a DataFrame, by replacing 'X' values with user input:

def modifyDF(variable):

    while df.isin(['X']).any().any():
        
        print('')
        x = input('row: ')
        y = input('col: ')
        v = input('value: ')
        print('')
        
        df.loc[x,y] = v
        print(df)

Now I want to ask the user for a file name, read the DataFrame, and ask the user to modify it. I tried calling the conversion function and saving the result like so:

variable = DataFrameConverter(file = input('File name: '))

and then passing it like so:

modifyDF(variable)

However, I get errors saying that df is not defined. What is wrong with the code? How can I make modifyDF use the DataFrame that was created by DataFrameConverter?

Asked By: prg_flks

||

Answers:

Without knowing the exact specifics of what you’re looking to achieve, I think what you’re getting at is wanting to return the dataframe from your first function to be used later.

import pandas as pd

def DataFrameConverter(file):
    T = pd.read_fwf(file, header=None, names=[
                    '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    T = T.rename(index={0: 'A', 1: 'B', 2: 'C', 3: 'D',
                 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I'})
    df = pd.DataFrame(T)
    print(df)
    return df

data_frame = DataFrameConverter(file=input('File name: '))

def modifyDF(data_frame):
    df = data_frame
    while df.isin(['X']).any().any():
        print('')
        x = input('row: ')
        y = input('col: ')
        v = input('value: ')
        print('')
        df.loc[x, y] = v
        print(df)

modifyDF(data_frame)

Or perhaps you want to use a global variable that each function accesses:

import pandas as pd

df: pd.DataFrame = None
def DataFrameConverter(file):
    T = pd.read_fwf(file, header=None, names=[
                    '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    T = T.rename(index={0: 'A', 1: 'B', 2: 'C', 3: 'D',
                 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I'})
    df = pd.DataFrame(T)
    print(df)

DataFrameConverter(file=input('File name: '))

def modifyDF():
    while df.isin(['X']).any().any():
        print('')
        x = input('row: ')
        y = input('col: ')
        v = input('value: ')
        print('')
        df.loc[x, y] = v
        print(df)

modifyDF()

Answered By: dalekman1234

Minimum to get what you want

In your DataFrameConverter function, you aren’t actually returning any results.

I think if you change your line:

print(df)

To be:

return (df)

You’ll get what you’re wanting.

Side Notes

  • Having a variable named ‘variable’ is literal eye cancer. Since it’s a pandas dataframe I would just store it as ‘df’ or ‘dataframe’.

  • DataFrameConverter is really just reading a pandas dataframe from a text file. So your code would be easier to follow if you had function names such as ‘read_data’ and ‘modify_data’.

Answered By: Kaleb Fenley