File size: 1,371 Bytes
8a79f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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