Last active
October 26, 2023 19:03
-
-
Save TristynAlxander/fcc1c343447c5f86eac57b298f2e5b35 to your computer and use it in GitHub Desktop.
This Function uses the pandas python module to convert csv files produced by akta pure machines into dictionaries of dataframes.
This file contains 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
# AKTA Pure | |
def akta_pure_csv_to_dataframe(csv_file_name): | |
""" | |
DESCRIPTION: | |
This Function uses the pandas python module to convert csv files produced by akta pure machines into dictionaries of dataframes. | |
ARGUMENTS: | |
csv_file_name = str: path to csv file produced by akta pure machine. | |
ERRORS: | |
None? | |
RETURN: | |
dataframe_dictionary ={ | |
"UV": <dataframe>, | |
... | |
} | |
EXAMPLE: | |
dataframe_dictionary = akta_pure_csv_to_dataframe("my_chromatogram.csv") | |
""" | |
original_df = pd.read_csv(csv_file_name, encoding='utf-16',sep='\t',skiprows=[0]) | |
# Dictionary for better names. | |
UNIT_TO_DIMENSION = { | |
"ml":"Volume (mL)", | |
"mAU":"Absorbance (arb. mAU)", | |
"mAU (1 cm)":"Absorbance (mAU at 1 cm)", | |
"mS/cm":"Conductivimty (mS/cm)", | |
"%":"Percent B (%)", | |
} | |
# Split Dataframe | |
dataframe_dictionary = {} | |
num_columns = original_df.shape[1] | |
for start_col in range(0, num_columns, 2): | |
# Split Dataframe by Columns | |
end_col = min(start_col + 2, num_columns) | |
df = original_df.iloc[:, start_col:end_col] | |
# Get the header of the first column | |
df_name = df.columns[0] | |
# Rename the columns using the first row as header units | |
df.columns = [ (UNIT_TO_DIMENSION.get(head) if UNIT_TO_DIMENSION.get(head) else head) for head in df.iloc[0]] | |
# Drop the first row since it's now the column headers | |
df = df[1:] | |
# Correct Values | |
try: | |
df = df.dropna(how='all') | |
df = df.replace(r'\n',' ', regex=True) | |
df = df.replace('Waste', 0) | |
df = df.replace('Frac', 0) | |
df = df.astype(float) | |
except ValueError: | |
print(f"Failed to cast database ({df_name}) to floats.") | |
pass | |
# Append to Dictionary | |
dataframe_dictionary[df_name] = df | |
# Return Dataframe Dictionary | |
return dataframe_dictionary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment