{ "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 }