这很可能是一个非常基本的问题,但我仍在学习类/对象/构造函数等。我正在尝试将其中的一些概念应用到我当前的工作流程中。
我正试图创建一个类,根据我指定的内容,将我的数据帧自动保存为CSV或xlsx文件,保存到给定的文件夹中。然而,我不相信我将数据帧作为对象属性正确地传递。这是我的代码:
award_date_change = merged_df.loc[merged_df['award_date_change'] == 'yes'] #this is my data frame
class uploading_to_GC:
def __init__(self, file_name, file_type, already_exists): #constructor where I want to pass my data frame, file type to be saved to, and specifying if the file already exists in my folder
self.file_name = file_name
self.file_type = file_type
self.already_exists = already_exists
def print_file_name(self):
self.file_name.head(5)
def private_workspace(self):
commonPath = os.path.expanduser(r"~path")
GCdocs = commonPath + '384593683' + '\'
path = GCdocs + "" + file_name
if len(self.file_name) != 0 and self.already_exists == True: #if a file already exists in Gfolder
if self.file_type == "csv": #for csv files
GC_old = pd.read_csv(path)
GC_new = GC_old.append(self.file_name, ignore_index=True)
GC_new.to_csv(path, index = False)
print("csv file is updated to private workspace in GCdocs")
elif self.file_type == "xlsx": #for xlsx files
GC_old = pd.read_csv(path)
GC_new = GC_old.append(self.file_name, ignore_index=True)
GC_new.to_excel(path, index = False)
print("excel file is updated to private workspace in GCdocs")
else:
print("unrecognized file type")
elif len(self.file_name) != 0 and self.already_exists == False: #if a file does FOLDER already exist in folder
if self.file_type == "csv":
self.file_name.to_csv(path,index=False)
if self.file_type == "xlsx":
self.file_name.to_excel(path,index=False)
else:
print("unrecognized file type")
else:
print("there is no data to upload")
award_date_change = uploading_to_GC(award_date_change,"csv", False)
award_date_change.private_workspace
我知道我不需要使用类来做到这一点,但我想挑战自己,开始更频繁地使用类。如有任何帮助,将不胜感激
您可以非常简单地将df作为数据成员传递并存储在Class
中:
class Foo:
def __init__(df: pd.DataFrame):
self.df = df
# or, if you want to be sure you don't modify the original df
self.df = df.copy()
df = pd.DataFrame()
foo_obj = Foo(df)
编辑:: pd.DataFrame
用于类型提示。这不会影响实际的代码,但仅对读者有用,因为我们期望将pd.DataFrame作为输入。如果你不传递DataFrame,好的IDE也会给你一个错误。