NetworkX experiments

This commit is contained in:
Tim
2023-10-23 20:44:43 +02:00
parent 5f4f732eb2
commit 077846d3be
3 changed files with 9930 additions and 89416 deletions

View File

@ -1,4 +1,4 @@
# Weekly *X*: DD.MM.YYYY # Weekly *12*: 12.10.2023
## Teilnehmer ## Teilnehmer
- Prof. Arinir - Prof. Arinir
@ -18,4 +18,5 @@
| Action Item | Verantwortlicher | Deadline | | Action Item | Verantwortlicher | Deadline |
|-------------|------------------|-----------------| |-------------|------------------|-----------------|
| Beispiel | Max Mustermann | nächstes Weekly | | Finanzdaten optimieren | Kim, Tristan | nächstes Weekly |
| Rebasen vom Branch für Sascha | Sascha | nächstes Weekly |

View File

@ -0,0 +1,834 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "68a5fc0a3d3f7c63",
"metadata": {
"collapsed": false
},
"source": [
"# Relation queries via SQLAlchemy for networkx"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b6eea59adeae27d4",
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-07T13:25:27.888866200Z",
"start_time": "2023-10-07T13:25:27.704284300Z"
},
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'c:\\\\Users\\\\trimr\\\\Projekte\\\\aki_prj23_transparenzregister'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os.path\n",
"\n",
"import pandas as pd\n",
"\n",
"if not os.path.exists(\"src\"):\n",
" %cd \"../\"\n",
"os.path.abspath(\".\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "eb9498d3",
"metadata": {},
"outputs": [],
"source": [
"from aki_prj23_transparenzregister.utils.sql import entities\n",
"from sqlalchemy.orm import aliased\n",
"from sqlalchemy import func, text\n",
"\n",
"# Alias for Company table for the base company\n",
"to_company = aliased(entities.Company, name=\"to_company\")\n",
"\n",
"# Alias for Company table for the head company\n",
"from_company = aliased(entities.Company, name=\"from_company\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6a317af6",
"metadata": {},
"outputs": [],
"source": [
"from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider\n",
"from aki_prj23_transparenzregister.utils.sql.connector import get_session\n",
"\n",
"session = get_session(JsonFileConfigProvider(\"secrets.json\"))"
]
},
{
"cell_type": "markdown",
"id": "814d5edb",
"metadata": {},
"source": [
"# All Company Relations"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "2d17651a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'SELECT to_company.id AS id_company_to, to_company.name AS name_company_to, relation.relation AS relation_type, from_company.name AS name_company_from, from_company.id AS id_company_from \\nFROM company AS to_company JOIN (relation JOIN company_relation ON relation.id = company_relation.id) ON relation.company_id = to_company.id JOIN company AS from_company ON company_relation.company2_id = from_company.id'"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Query to fetch relations between companies\n",
"relations_company_query = (\n",
" session.query(\n",
" to_company.id.label(\"id_company_to\"),\n",
" to_company.name.label(\"name_company_to\"),\n",
" entities.CompanyRelation.relation.label(\"relation_type\"),\n",
" from_company.name.label(\"name_company_from\"),\n",
" from_company.id.label(\"id_company_from\"),\n",
" )\n",
" .join(\n",
" entities.CompanyRelation,\n",
" entities.CompanyRelation.company_id == to_company.id,\n",
" )\n",
" .join(\n",
" from_company,\n",
" entities.CompanyRelation.company2_id == from_company.id,\n",
" )\n",
")\n",
"str(relations_company_query)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f1f0f2de",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"89.3 ms ± 1.99 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"# str(relations_query.filter((entities.CompanyRelation.company_id == 2) or (entities.CompanyRelation.company2_id == 2)))\n",
"%timeit pd.read_sql_query(str(relations_company_query), session.bind)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "444cd402",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id_company_to</th>\n",
" <th>name_company_to</th>\n",
" <th>relation_type</th>\n",
" <th>name_company_from</th>\n",
" <th>id_company_from</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>2. Schaper Objekt GmbH &amp; Co. Kiel KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>Multi-Center Warenvertriebs GmbH</td>\n",
" <td>2213</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>32</td>\n",
" <td>Alb-Windkraft GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>EnBW Windkraftprojekte GmbH</td>\n",
" <td>845</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>34</td>\n",
" <td>Anneliese Köster GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>INDUS Holding Aktiengesellschaft</td>\n",
" <td>1903</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>74</td>\n",
" <td>AURELIUS Equity Opportunities SE &amp; Co. KGaA</td>\n",
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
" <td>AURELIUS Management SE</td>\n",
" <td>163</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>77</td>\n",
" <td>Aurelius KG</td>\n",
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
" <td>Aurelius Verwaltungs GmbH</td>\n",
" <td>80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>573</th>\n",
" <td>3137</td>\n",
" <td>Zalando BTD 011 SE &amp; Co. KG</td>\n",
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
" <td>Zalando SE</td>\n",
" <td>3112</td>\n",
" </tr>\n",
" <tr>\n",
" <th>574</th>\n",
" <td>3137</td>\n",
" <td>Zalando BTD 011 SE &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>Zalando Operations GmbH</td>\n",
" <td>3103</td>\n",
" </tr>\n",
" <tr>\n",
" <th>575</th>\n",
" <td>3138</td>\n",
" <td>zLabels Creation &amp; Sales GmbH &amp; Co. KG</td>\n",
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
" <td>zLabels GmbH</td>\n",
" <td>3113</td>\n",
" </tr>\n",
" <tr>\n",
" <th>576</th>\n",
" <td>3145</td>\n",
" <td>Zalando Customer Care International SE &amp; Co. KG</td>\n",
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
" <td>Zalando SE</td>\n",
" <td>3112</td>\n",
" </tr>\n",
" <tr>\n",
" <th>577</th>\n",
" <td>3145</td>\n",
" <td>Zalando Customer Care International SE &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>Zalando Operations GmbH</td>\n",
" <td>3103</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>578 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" id_company_to name_company_to \\\n",
"0 5 2. Schaper Objekt GmbH & Co. Kiel KG \n",
"1 32 Alb-Windkraft GmbH & Co. KG \n",
"2 34 Anneliese Köster GmbH & Co. KG \n",
"3 74 AURELIUS Equity Opportunities SE & Co. KGaA \n",
"4 77 Aurelius KG \n",
".. ... ... \n",
"573 3137 Zalando BTD 011 SE & Co. KG \n",
"574 3137 Zalando BTD 011 SE & Co. KG \n",
"575 3138 zLabels Creation & Sales GmbH & Co. KG \n",
"576 3145 Zalando Customer Care International SE & Co. KG \n",
"577 3145 Zalando Customer Care International SE & Co. KG \n",
"\n",
" relation_type name_company_from \\\n",
"0 KOMMANDITIST Multi-Center Warenvertriebs GmbH \n",
"1 KOMMANDITIST EnBW Windkraftprojekte GmbH \n",
"2 KOMMANDITIST INDUS Holding Aktiengesellschaft \n",
"3 HAFTENDER_GESELLSCHAFTER AURELIUS Management SE \n",
"4 HAFTENDER_GESELLSCHAFTER Aurelius Verwaltungs GmbH \n",
".. ... ... \n",
"573 HAFTENDER_GESELLSCHAFTER Zalando SE \n",
"574 KOMMANDITIST Zalando Operations GmbH \n",
"575 HAFTENDER_GESELLSCHAFTER zLabels GmbH \n",
"576 HAFTENDER_GESELLSCHAFTER Zalando SE \n",
"577 KOMMANDITIST Zalando Operations GmbH \n",
"\n",
" id_company_from \n",
"0 2213 \n",
"1 845 \n",
"2 1903 \n",
"3 163 \n",
"4 80 \n",
".. ... \n",
"573 3112 \n",
"574 3103 \n",
"575 3113 \n",
"576 3112 \n",
"577 3103 \n",
"\n",
"[578 rows x 5 columns]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"company_relations = pd.read_sql_query(str(relations_company_query), session.bind)\n",
"company_relations"
]
},
{
"cell_type": "markdown",
"id": "b27d6532",
"metadata": {},
"source": [
"# All person relations"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "52af1d30",
"metadata": {},
"outputs": [],
"source": [
"relations_person_query = (\n",
" session.query(\n",
" entities.Company.id.label(\"id_company\"),\n",
" entities.Company.name.label(\"name_company\"),\n",
" entities.PersonRelation.relation.label(\"relation_type\"),\n",
" entities.Person.id.label(\"id_person\"),\n",
" entities.Person.lastname.label(\"lastname\"),\n",
" entities.Person.firstname.label(\"firstname\"),\n",
" entities.Person.date_of_birth.label(\"date_of_birth\"),\n",
" )\n",
" .join(\n",
" entities.PersonRelation,\n",
" entities.PersonRelation.company_id == entities.Company.id,\n",
" )\n",
" .join(\n",
" entities.Person,\n",
" entities.PersonRelation.person_id == entities.Person.id,\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "4f5ab3b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"259 ms ± 23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit pd.read_sql_query(str(relations_person_query), session.bind)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "c78b3e65",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id_company</th>\n",
" <th>name_company</th>\n",
" <th>relation_type</th>\n",
" <th>id_person</th>\n",
" <th>lastname</th>\n",
" <th>firstname</th>\n",
" <th>date_of_birth</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>0 10 24 Telefondienste GmbH</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>1</td>\n",
" <td>Tetau</td>\n",
" <td>Nicolas</td>\n",
" <td>1971-01-02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>0 10 24 Telefondienste GmbH</td>\n",
" <td>PROKURIST</td>\n",
" <td>2</td>\n",
" <td>Dammast</td>\n",
" <td>Lutz</td>\n",
" <td>1966-12-06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>3</td>\n",
" <td>Tutsch</td>\n",
" <td>Rosemarie</td>\n",
" <td>1941-10-09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>4</td>\n",
" <td>Staiger</td>\n",
" <td>Marc</td>\n",
" <td>1969-10-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>5</td>\n",
" <td>Staiger</td>\n",
" <td>Michaela</td>\n",
" <td>1971-03-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14891</th>\n",
" <td>3144</td>\n",
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>878</td>\n",
" <td>Weirich</td>\n",
" <td>Torsten</td>\n",
" <td>1975-07-21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14892</th>\n",
" <td>3144</td>\n",
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>1840</td>\n",
" <td>Brusinski</td>\n",
" <td>Bastian</td>\n",
" <td>1980-10-29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14893</th>\n",
" <td>3145</td>\n",
" <td>Zalando Customer Care International SE &amp; Co. KG</td>\n",
" <td>PROKURIST</td>\n",
" <td>9359</td>\n",
" <td>Pape</td>\n",
" <td>Ute</td>\n",
" <td>1978-12-13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14894</th>\n",
" <td>3146</td>\n",
" <td>zebotec GmbH</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>9628</td>\n",
" <td>Neff</td>\n",
" <td>Werner</td>\n",
" <td>1981-11-24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14895</th>\n",
" <td>3146</td>\n",
" <td>zebotec GmbH</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>9629</td>\n",
" <td>Morris</td>\n",
" <td>Richard</td>\n",
" <td>1971-01-02</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>14896 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" id_company name_company \\\n",
"0 1 0 10 24 Telefondienste GmbH \n",
"1 1 0 10 24 Telefondienste GmbH \n",
"2 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"3 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"4 2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"... ... ... \n",
"14891 3144 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
"14892 3144 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
"14893 3145 Zalando Customer Care International SE & Co. KG \n",
"14894 3146 zebotec GmbH \n",
"14895 3146 zebotec GmbH \n",
"\n",
" relation_type id_person lastname firstname date_of_birth \n",
"0 GESCHAEFTSFUEHRER 1 Tetau Nicolas 1971-01-02 \n",
"1 PROKURIST 2 Dammast Lutz 1966-12-06 \n",
"2 KOMMANDITIST 3 Tutsch Rosemarie 1941-10-09 \n",
"3 KOMMANDITIST 4 Staiger Marc 1969-10-22 \n",
"4 KOMMANDITIST 5 Staiger Michaela 1971-03-03 \n",
"... ... ... ... ... ... \n",
"14891 GESCHAEFTSFUEHRER 878 Weirich Torsten 1975-07-21 \n",
"14892 GESCHAEFTSFUEHRER 1840 Brusinski Bastian 1980-10-29 \n",
"14893 PROKURIST 9359 Pape Ute 1978-12-13 \n",
"14894 GESCHAEFTSFUEHRER 9628 Neff Werner 1981-11-24 \n",
"14895 GESCHAEFTSFUEHRER 9629 Morris Richard 1971-01-02 \n",
"\n",
"[14896 rows x 7 columns]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person_relations = pd.read_sql_query(str(relations_person_query), session.bind)\n",
"person_relations"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "9887d1f0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id_company</th>\n",
" <th>name_company</th>\n",
" <th>relation_type</th>\n",
" <th>id_person</th>\n",
" <th>lastname</th>\n",
" <th>firstname</th>\n",
" <th>date_of_birth</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>c_1</td>\n",
" <td>0 10 24 Telefondienste GmbH</td>\n",
" <td>GESCHAEFTSFUEHRER</td>\n",
" <td>p_1</td>\n",
" <td>Tetau</td>\n",
" <td>Nicolas</td>\n",
" <td>1971-01-02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>c_1</td>\n",
" <td>0 10 24 Telefondienste GmbH</td>\n",
" <td>PROKURIST</td>\n",
" <td>p_2</td>\n",
" <td>Dammast</td>\n",
" <td>Lutz</td>\n",
" <td>1966-12-06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>c_2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>p_3</td>\n",
" <td>Tutsch</td>\n",
" <td>Rosemarie</td>\n",
" <td>1941-10-09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>c_2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>p_4</td>\n",
" <td>Staiger</td>\n",
" <td>Marc</td>\n",
" <td>1969-10-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>c_2</td>\n",
" <td>1. Staiger Grundstücksverwaltung GmbH &amp; Co. KG</td>\n",
" <td>KOMMANDITIST</td>\n",
" <td>p_5</td>\n",
" <td>Staiger</td>\n",
" <td>Michaela</td>\n",
" <td>1971-03-03</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id_company name_company \\\n",
"0 c_1 0 10 24 Telefondienste GmbH \n",
"1 c_1 0 10 24 Telefondienste GmbH \n",
"2 c_2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"3 c_2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"4 c_2 1. Staiger Grundstücksverwaltung GmbH & Co. KG \n",
"\n",
" relation_type id_person lastname firstname date_of_birth \n",
"0 GESCHAEFTSFUEHRER p_1 Tetau Nicolas 1971-01-02 \n",
"1 PROKURIST p_2 Dammast Lutz 1966-12-06 \n",
"2 KOMMANDITIST p_3 Tutsch Rosemarie 1941-10-09 \n",
"3 KOMMANDITIST p_4 Staiger Marc 1969-10-22 \n",
"4 KOMMANDITIST p_5 Staiger Michaela 1971-03-03 "
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person_relations['id_company'] = person_relations['id_company'].apply(lambda x: f\"c_{x}\")\n",
"person_relations['id_person'] = person_relations['id_person'].apply(lambda x: f\"p_{x}\")\n",
"person_relations.head()"
]
},
{
"cell_type": "markdown",
"id": "f448841c",
"metadata": {},
"source": [
"# Person relations filtered and fitted that are nodes not leaves"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "52dad02d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>person_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2520</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4993</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3202</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4611</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4095</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1804</th>\n",
" <td>3565</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1805</th>\n",
" <td>3510</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1806</th>\n",
" <td>530</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1807</th>\n",
" <td>536</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1808</th>\n",
" <td>4617</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1809 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" person_id\n",
"0 2520\n",
"1 4993\n",
"2 3202\n",
"3 4611\n",
"4 4095\n",
"... ...\n",
"1804 3565\n",
"1805 3510\n",
"1806 530\n",
"1807 536\n",
"1808 4617\n",
"\n",
"[1809 rows x 1 columns]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sqlalchemy import func, text\n",
"\n",
"# Subquery to group and count the relations without joins\n",
"grouped_relations_subquery = (\n",
" session.query(\n",
" entities.PersonRelation.person_id,\n",
" )\n",
" .group_by(entities.PersonRelation.person_id)\n",
" .having(func.count() > 1)\n",
")\n",
"pd.DataFrame(grouped_relations_subquery.all())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}