I have a class in Python which takes in a dataset, and I have columns in this dataset which need to be converted from strings to floats. Since every function inside this class needs these columns in float format I was wondering if there was an efficient way to update these columns without having to convert them from string to float in each function.
For example, my code looks similar to this:
class xyz():
def __init__(self):
self.data = []
def xyzf1(self, data):
data["sales"] = pd.to_numeric(data["sales"])
data["prices"] = pd.to_numeric(data["prices"])
data["client"] = pd.to_numeric(data["client"])
...
def xyzf2(self, data):
data["sales"] = pd.to_numeric(data["sales"])
data["prices"] = pd.to_numeric(data["prices"])
data["client"] = pd.to_numeric(data["client"])
...
def xyzf3(self, data):
data["sales"] = pd.to_numeric(data["sales"])
data["prices"] = pd.to_numeric(data["prices"])
data["client"] = pd.to_numeric(data["client"])
...
Basically, I wanted to know if there's a neater way of converting the "sales" column to a numeric column without having to include that line in every single function inside the class.
CodePudding user response:
Firstly, I think that type of data is dict (cause you use the key "sales"). Am I correct? May be no, because I didn`t work with pandas (may be it is a specificity of pandas).
Return to subjest. May be my version will tell you something:
class xyz():
def __init__(self):
self.data = {"sales": "222"}
self.data["sales"] = int(self. data["sales"])
def xyzf1(self, data):
print(type(self.data["sales"]))
def xyzf2(self, data):
print(type(self.data["sales"]))
def xyzf3(self, data):
print(type(self.data["sales"]))
if __name__ == "__main__":
data = {"sales": "111"}
x = xyz()
x.xyzf1(data) # <class 'int'>
x.xyzf2(data) # <class 'int'>
x.xyzf3(data) # <class 'int'>
print(data["sales"]) # 111
CodePudding user response:
The full functionality of OOPS is not properly used in the code
Instead of passing the data to individual methods in the class, pass the argument directly to the class and perform the preprocessing. This can be done by using the __init__()
method
The data can be used in the class methods by using self.data
class xyz():
def __init__(self, data):
self.data = data
self.data["sales"] = pd.to_numeric(self.data["sales"])
def xyzf1(self):
#for example if you want to get the first row in the dataset
first_row = self.data.iloc[0]
return first_row
...
def xyzf2(self):
#for example if you want to get the second row in the dataset
second_row = self.data.iloc[0]
return second_row
...
def xyzf3(self):
...
instance = xyz(data)
row1 = instance.xyzf1()
row2 = instance.xyzf2()