|
18 | 18 | ------
|
19 | 19 | """
|
20 | 20 |
|
21 |
| -import pandas as pd |
22 | 21 | from pathlib import Path
|
23 | 22 |
|
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 |
27 | 24 |
|
28 | 25 | # Get the path of the current script file
|
29 | 26 | script_path = Path(__file__)
|
30 | 27 | print(script_path)
|
31 | 28 |
|
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 |
33 | 34 | 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 | +# ############################# |
34 | 39 |
|
35 | 40 | # Load the CSV RF into a DataFrame
|
36 | 41 | df = pd.read_csv(Path(file_path) / str(csv_file_to_update_name + '.csv'))
|
37 | 42 |
|
38 | 43 | # Find unique values in Equipment that have no code and are not None or empty
|
39 | 44 | 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() |
41 | 46 |
|
42 | 47 | # Create a mapping of unique values to codes
|
43 | 48 | value_to_code = {}
|
44 | 49 | # 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 |
47 | 52 | else:
|
48 | 53 | next_code = 0
|
49 | 54 |
|
50 | 55 | # Iterate through unique values
|
51 | 56 | for value in unique_values:
|
52 | 57 | # 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() |
54 | 59 | if not matching_rows.empty:
|
55 | 60 | # Use the existing code for this value
|
56 | 61 | existing_code = int(matching_rows.iloc[0])
|
|
60 | 65 | existing_code = next_code
|
61 | 66 | next_code += 1
|
62 | 67 | 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 |
65 | 70 |
|
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 |
68 | 73 |
|
69 | 74 | # Save CSV with equipment codes
|
70 | 75 | df.to_csv(Path(file_path) / str(csv_file_to_update_name + '_new.csv'), index=False)
|
0 commit comments