File size: 10,182 Bytes
aaa15e2 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2a56cf68-b4bb-4d64-989c-7cbc6a5ffcab",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "a34bf8b7-c3ac-443f-9377-7b0244ff4405",
"metadata": {},
"source": [
"### merge.csv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b7d3de23-c1ae-4cdf-b90d-84a21ccbe288",
"metadata": {},
"outputs": [],
"source": [
"def read_csv(file_path):\n",
" if not os.path.exists(file_path):\n",
" print(\"Error: File not found.\")\n",
" return None\n",
" else:\n",
" df = pd.read_csv(file_path)\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cd134954-2da9-44ac-8943-c5cabc9503d0",
"metadata": {},
"outputs": [],
"source": [
"def save_csv(dataframe, file_path):\n",
" try:\n",
" dataframe.to_csv(file_path, index=False)\n",
" print(\"CSV file saved successfully.\")\n",
" except Exception as e:\n",
" print(f\"Error saving CSV file: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9d04fbcb-1ea3-4b77-b581-576ad28d27c7",
"metadata": {},
"outputs": [],
"source": [
"def delete_0_status(data_frame, column_name=\"Status\"):\n",
" try:\n",
" data_frame = data_frame[data_frame[column_name] != 0]\n",
" return data_frame\n",
" except Exception as e:\n",
" print(f\"Error deleting rows with status zero: {e}\")\n",
" return None\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c7127d9c-a21b-4fab-abf5-c5fcc94df9b8",
"metadata": {},
"outputs": [],
"source": [
"def merge(file1, file2):\n",
" df1 = read_csv(file1) \n",
" df2 = read_csv(file2)\n",
"\n",
" same_rows = pd.merge(df1, df2, how=\"inner\")\n",
" print(\"same rows:\", same_rows)\n",
"\n",
" merged_df = pd.merge(df1, df2, on=list(df1.columns), how='outer')\n",
" # show duplicated row\n",
" duplicates = merged_df[merged_df.duplicated()]\n",
" if not duplicates.empty:\n",
" print(duplicates)\n",
" else:\n",
" print(\"no duplicate row\")\n",
" \n",
" # drop duplicata rows\n",
" merged_df.drop_duplicates(inplace=True, keep=\"first\")\n",
" \n",
" return merged_df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "07a066ef-1cd9-454a-8494-ee195601a0d1",
"metadata": {},
"outputs": [],
"source": [
"def type_reform(df, column_name=\"Type of Report\"):\n",
" # construct report_type: tcfd -> 1, none_tcfd->0\n",
"\n",
" new_column_name = \"tcfd\"\n",
" df[column_name] = df[column_name].fillna(\"\") # del nan \n",
" df[new_column_name] = df[column_name].apply(lambda x: 1 if x == 'TCFD' else (0 if 'TCFD' not in x else -1))\n",
" \n",
" # delete status == 0 row\n",
" df = delete_0_status(df, column_name=\"Status\")\n",
"\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "23549957-4b26-40c1-9aed-b879775924f6",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"same rows: Company Report URL \\\n",
"0 Wolfspeed https://assets.wolfspeed.com/uploads/2022/10/W... \n",
"1 Aurizon https://www.aurizon.com.au/sustainability \n",
"\n",
" Type of Report Year Published \\\n",
"0 TCFD,SASB,GRI,UN_SDGs 2022 \n",
"1 TCFD 2020 \n",
"\n",
" Industry Geography \\\n",
"0 Energy Equipment and Services, Manufacturing United States of America \n",
"1 Rail Transportation Australia \n",
"\n",
" Status \n",
"0 1 \n",
"1 1 \n",
" Company Report URL \\\n",
"80 Fortum Oyj https://www.fortum.com/about-us/investors/repo... \n",
"81 Fortum Oyj https://www.fortum.com/about-us/investors/repo... \n",
"117 AcBel Polytech https://www.acbel.com.tw/en/csr-report-download \n",
"\n",
" Type of Report Year Published Industry Geography \\\n",
"80 TCFD, GRI 2020 Electric and Gas Utilities Finland \n",
"81 TCFD, GRI 2020 Electric and Gas Utilities Finland \n",
"117 GRI, SASB 2020 Capital Goods Taiwan \n",
"\n",
" Status \n",
"80 1 \n",
"81 1 \n",
"117 1 \n",
"CSV file saved successfully.\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
" # merge two files\n",
" esg = \"./input/esg_new.csv\" # (123, 7)\n",
" tcfd = \"./input/tcfd_new.csv\" # (118, 7)\n",
" merge_df = merge(esg, tcfd)\n",
"\n",
" merged_file = \"./output/merge.csv\" # (187, 8)\n",
" reform_df = type_reform(merge_df)\n",
" save_csv(reform_df, merged_file)"
]
},
{
"cell_type": "markdown",
"id": "fccdee41-08b9-46f6-912c-2867705b4da1",
"metadata": {},
"source": [
"### save merge_onehot.csv"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "6a7f0360-e48c-4d09-a268-be9e8f730887",
"metadata": {},
"outputs": [],
"source": [
"def one_hot(df, column_name=\"Type of Report\"):\n",
" # construct report_type: tcfd -> 1, none_tcfd->0\n",
" # df = pd.read_csv(input_file)\n",
"\n",
" column_data = df[column_name]\n",
"\n",
" # split \",\"\n",
" split_contents = set()\n",
" for item in column_data.str.split(','):\n",
" if isinstance(item, list):\n",
" split_contents.update(filter(None, map(str.strip, item)))\n",
"\n",
" print(split_contents)\n",
" for content in split_contents:\n",
" new_column_name = f\"{content.strip()}\"\n",
" print(\"new_column_name: \", new_column_name)\n",
" df[new_column_name] = df.apply(lambda row: 1 if isinstance(row[column_name], str) and new_column_name in [x.strip() for x in row[column_name].split(',')] else 0, axis=1)\n",
" \n",
" # delete status == 0 row\n",
" df = delete_0_status(df, column_name=\"Status\")\n",
"\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e03d0cb8-c558-482d-b95e-4cbc03506693",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"same rows: Company Report URL \\\n",
"0 Wolfspeed https://assets.wolfspeed.com/uploads/2022/10/W... \n",
"1 Aurizon https://www.aurizon.com.au/sustainability \n",
"\n",
" Type of Report Year Published \\\n",
"0 TCFD,SASB,GRI,UN_SDGs 2022 \n",
"1 TCFD 2020 \n",
"\n",
" Industry Geography \\\n",
"0 Energy Equipment and Services, Manufacturing United States of America \n",
"1 Rail Transportation Australia \n",
"\n",
" Status \n",
"0 1 \n",
"1 1 \n",
" Company Report URL \\\n",
"80 Fortum Oyj https://www.fortum.com/about-us/investors/repo... \n",
"81 Fortum Oyj https://www.fortum.com/about-us/investors/repo... \n",
"117 AcBel Polytech https://www.acbel.com.tw/en/csr-report-download \n",
"\n",
" Type of Report Year Published Industry Geography \\\n",
"80 TCFD, GRI 2020 Electric and Gas Utilities Finland \n",
"81 TCFD, GRI 2020 Electric and Gas Utilities Finland \n",
"117 GRI, SASB 2020 Capital Goods Taiwan \n",
"\n",
" Status \n",
"80 1 \n",
"81 1 \n",
"117 1 \n",
"{'SDGs', 'TNFD', 'TCFD', 'GRI', 'UN_SDGs', 'SFDR', 'NFRD', 'NGFS', 'UNGC', 'SASB'}\n",
"new_column_name: SDGs\n",
"new_column_name: TNFD\n",
"new_column_name: TCFD\n",
"new_column_name: GRI\n",
"new_column_name: UN_SDGs\n",
"new_column_name: SFDR\n",
"new_column_name: NFRD\n",
"new_column_name: NGFS\n",
"new_column_name: UNGC\n",
"new_column_name: SASB\n",
"CSV file saved successfully.\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
" # merge files\n",
" esg = \"./input/esg_new.csv\" # (123, 7)\n",
" tcfd = \"./input/tcfd_new.csv\" # (118, 7)\n",
" merge_df2 = merge(esg, tcfd)\n",
" \n",
" # process merge file\n",
" final_df = one_hot(merge_df2)\n",
" merged_file = \"./output/merge_onehot.csv\" # (187, )\n",
" save_csv(final_df, merged_file)"
]
},
{
"cell_type": "markdown",
"id": "bf378765-3832-4106-a079-4e77b6d05c31",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|