Last active
November 16, 2019 06:16
-
-
Save tedsteinmann/0af9d6c9e7caaee7c7faa7359009fe3a to your computer and use it in GitHub Desktop.
Basic python class for reading data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = { | |
"raw": "../data/raw/", | |
"processed": "../data/processed/", | |
"lookup": "../data/lookup/" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from classes.config import data | |
class Map: | |
def __init__(self,file_name, index): | |
path = data["lookup"] | |
self.index = index | |
self.mappings = _load_mappings(path,file_name) | |
def map_data(self,df,df_index): | |
return pd.merge(df, self.mappings, left_on=df_index, right_on=self.index) | |
def _load_mappings(path,file_name): | |
return pd.read_csv(path + file_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from classes.config import data | |
class Read: | |
def __init__(self,file_name): | |
path = data["raw"] | |
self.df = _read_data(path,file_name) | |
# get data from csv | |
def _read_data(path,file_name): | |
return pd.read_csv(path + file_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from classes.config import data | |
class Write: | |
def __init__(self,df,file_name): | |
self.path = data["processed"] | |
self._write_data(df,file_name) | |
# write data to csv | |
def _write_data(self,df,file_name): | |
df.to_csv(self.path + file_name + '.csv', sep='\t') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment