2

I want to create a class that includes some functions, that one of them call another one in class, something like that:

import pandas as pd


class Prep:
    def __init__(self, data):
        self.data = data

    def slicing(self):
        sliceInput = self.data.iloc[:, 1:8]
        sliceTarget = self.data.iloc[:, 8]
        return sliceInput, sliceTarget

    def convert(self):
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == "__main__":
    data_frame = pd.read_csv('data_manual.csv', sep=';')

    tes = Prep(data_frame)
    print(tes.convert())

i got error like this NameError: name 'sliceInput' is not defined

how to call convertInput and convertTarget in function why I get an error, I don't understand at all.

1
  • may be self.sliceInput = self.data.iloc[:, 1:8] and self.sliceTarget = self.data.iloc[:, 8] will do the trick Commented Jan 4, 2020 at 4:33

2 Answers 2

2

You need to add sliceInput, sliceTarget = self.slicing() in covert

import pandas as pd


class Prep:
    def __init__(self, data):
        self.data = data

    def slicing(self):
        sliceInput = self.data.iloc[:, 1:8]
        sliceTarget = self.data.iloc[:, 8]

        return sliceInput, sliceTarget

    def convert(self):
        sliceInput, sliceTarget = self.slicing()
        convertInput = sliceInput.to_numpy(float)
        convertTarget = sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == "__main__":
    data_frame = pd.read_csv('data_manual.csv', sep=';')

    tes = Prep(data_frame)
    print(tes.convert())
Sign up to request clarification or add additional context in comments.

Comments

2

Just add self to the defined attributes. Also, because data is immutable after you have instantiated a Prep object, it would be better for performance if you just initialize the slice variables once in the __init__() method.

import pandas as pd


class Prep:
    def __init__(self, data):
        self.data = data
        self.sliceInput = data.iloc[:, 1:8]
        self.sliceTarget = data.iloc[:, 8]

    def slicing(self):
        return self.sliceInput, self.sliceTarget


    def convert(self):
        convertInput = self.sliceInput.to_numpy(float)
        convertTarget = self.sliceTarget.to_numpy(int)
        return convertInput, convertTarget

if __name__ == "__main__":
    data_frame = pd.read_csv('data_manual.csv', sep=';')

    tes = Prep(data_frame)
    print(tes.convert())

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.