Skip to content

Commit a30017a

Browse files
committed
codes_to_items_list: script generalised + PEP 8
1 parent b6cc514 commit a30017a

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/tlo/analysis/codes_to_items_list.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,44 @@
1818
------
1919
"""
2020

21-
import pandas as pd
2221
from pathlib import Path
2322

24-
25-
# ## CHANGE THIS IF YOU WANT TO USE DIFFERENT FILE AS INPUT
26-
csv_file_to_update_name = 'ResourceFile_Equipment_withoutEquipmentCodes'
23+
import pandas as pd
2724

2825
# Get the path of the current script file
2926
script_path = Path(__file__)
3027
print(script_path)
3128

32-
# Specify the file path to RF csv file
29+
# #############################
30+
# ## CHANGE THIS FOR YOUR FILE
31+
# Specify name of the csv file
32+
csv_file_to_update_name = 'ResourceFile_Equipment_withoutEquipmentCodes'
33+
# Specify the file path to csv file
3334
file_path = script_path.parent.parent.parent.parent / 'resources/healthsystem/infrastructure_and_equipment'
35+
# Specify the names of columns containing the item names and item codes
36+
item_col_name = 'Equip_Item'
37+
code_col_name = 'Equip_Code'
38+
# #############################
3439

3540
# Load the CSV RF into a DataFrame
3641
df = pd.read_csv(Path(file_path) / str(csv_file_to_update_name + '.csv'))
3742

3843
# Find unique values in Equipment that have no code and are not None or empty
3944
unique_values =\
40-
df.loc[df['Equip_Code'].isna() & df['Equip_Item'].notna() & (df['Equip_Item'] != ''), 'Equip_Item'].unique()
45+
df.loc[df[code_col_name].isna() & df[item_col_name].notna() & (df[item_col_name] != ''), item_col_name].unique()
4146

4247
# Create a mapping of unique values to codes
4348
value_to_code = {}
4449
# Initialize the starting code value
45-
if not df['Equip_Code'].isna().all():
46-
next_code = int(df['Equip_Code'].max()) + 1
50+
if not df[code_col_name].isna().all():
51+
next_code = int(df[code_col_name].max()) + 1
4752
else:
4853
next_code = 0
4954

5055
# Iterate through unique values
5156
for value in unique_values:
5257
# Check if there is at least one existing code for this value
53-
matching_rows = df.loc[df['Equip_Item'] == value, 'Equip_Code'].dropna()
58+
matching_rows = df.loc[df[item_col_name] == value, code_col_name].dropna()
5459
if not matching_rows.empty:
5560
# Use the existing code for this value
5661
existing_code = int(matching_rows.iloc[0])
@@ -60,11 +65,11 @@
6065
existing_code = next_code
6166
next_code += 1
6267
value_to_code[value] = existing_code
63-
# Update the 'Equip_Code' column for matching rows
64-
df.loc[df['Equip_Item'] == value, 'Equip_Code'] = existing_code
68+
# Update the code_col_name column for matching rows
69+
df.loc[df[item_col_name] == value, code_col_name] = existing_code
6570

66-
# Convert 'Equip_Code' column to integers
67-
df['Equip_Code'] = df['Equip_Code'].astype('Int64') # Convert to nullable integer type
71+
# Convert code_col_name column to integers
72+
df[code_col_name] = df[code_col_name].astype('Int64') # Convert to nullable integer type
6873

6974
# Save CSV with equipment codes
7075
df.to_csv(Path(file_path) / str(csv_file_to_update_name + '_new.csv'), index=False)

0 commit comments

Comments
 (0)