Search code examples
importerror

import class with pandas from file


I have two files: main.py

from personal_function.Convert import cls_Convert

df_key = 'PERFIL'

a = cls_Convert(df_data_2)
a.convert_cat_to_num(df_key)
df_data_2 = a.df

personal_function/Convert.py

import pandas as pd

class cls_Convert:
    def __init__(self,df):
        self.df = df

    # Mudar variavel categorica para numerica
    def convert_cat_to_num(self,df_key):
        self.df[df_key] = pd.factorize(self.df[df_key],sort=True)[0] + 1
        return self.df

    # Mudar variavel numerica para categorica
    def convert_num_to_cat(self,df_key,cat_bin,cat_label):
        self.df[df_key].replace(to_replace = cat_bin, value =cat_label, inplace=True)
        return self.df

however I get this error

ImportError: cannot import name 'cls_Convert' from 'personal_function.Convert'


Solution

  • I need to use

    from os import getcwd
    from sys import path
    cwd = getcwd()
    path.append(cwd)
    

    than use the whole absolute path