anonymous-submission-acl2025's picture
add 17
8a79f2e
import os
import pandas as pd
import string
from lib.utilities.labeler import Labeler
from lib.utilities import codebook
class Stata():
def general_exporter(self,clean_master_df: pd.DataFrame, cb_dict: dict, level_name: str, is_wide: bool):
exportable_df = Labeler(sheet=level_name, is_wide=is_wide, codebook_dict=cb_dict).add_labels_to_df(clean_master_df)
if level_name == "User":
codebook.create_expanded_codebook(exportable_df)
exportable_df = self.remove_bad_chars(exportable_df)
exportable_df = self.stata_reformat(exportable_df)
exportable_df.to_csv(os.path.join("data","external","intermediate", f"PrepAnalysis{level_name}.csv"),
index=False)
print("exported dataframe for stata")
"""removes strange chars and converts everything to strings"""
def remove_bad_chars(self, df):
df = df.astype(str).applymap(lambda x: x.strip().replace("\n", "").replace('"', ''))
df = df.applymap(lambda x: ''.join([y for y in x if y in string.printable]))
return df
"""export separate csv file for stata analysis
- ensures that each cell takes the first 81 chars
"""
def stata_reformat(self,exportable_master):
stata_export_df = exportable_master.applymap(lambda x: x[0:81])
return stata_export_df