Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ssp24 committed Sep 3, 2024
1 parent 4f9fbb3 commit 0e0ea07
Show file tree
Hide file tree
Showing 3 changed files with 667 additions and 0 deletions.
127 changes: 127 additions & 0 deletions workshops/2024_Digital_History_Halle/02_Autoren_extrahieren.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "56184c51-b4cd-4009-b53c-40f73bb4e14a",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22cfe659-cb0d-42c4-830f-d10deb947f4e",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"Studentenbewegung.csv\", encoding=\"utf-8\")\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49e471cb-3e2f-46ae-8b44-3707b8dd5d79",
"metadata": {},
"outputs": [],
"source": [
"df = df.drop(['titel', 'rela'], axis=1)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b37bdbf-cf0f-4ebe-a1f3-3d0406a411dc",
"metadata": {},
"outputs": [],
"source": [
"df = df[df['author'] != 'unknown']\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "54b7dbc7-4bb0-42b2-b642-c97dcbf6b3fe",
"metadata": {},
"source": [
"### Bereinigen: "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2b544f6-c1d9-4391-8fe5-12815a31a09d",
"metadata": {},
"outputs": [],
"source": [
"df.loc[:, 'gnd_modified'] = df['gnd'].str.split(';').str.get(0)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83716027-df06-4469-a03e-b40905f937b0",
"metadata": {},
"outputs": [],
"source": [
"df = df.drop(['gnd'], axis=1)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66378256-9230-44b8-9243-026c7ca5e171",
"metadata": {},
"outputs": [],
"source": [
"count_unknown = (df['gnd_modified'] == 'unknown').sum()\n",
"print(\"Einträge in gnd_modified mit value 'unknown': \", count_unknown)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b37202ea-4067-483f-bd93-41f4d6dc43a3",
"metadata": {},
"outputs": [],
"source": [
"df.to_csv(\"authors.csv\", encoding=\"utf-8\", index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffad82ee-3945-4a99-a11b-b36f127500df",
"metadata": {},
"outputs": [],
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
180 changes: 180 additions & 0 deletions workshops/2024_Digital_History_Halle/04_Kartendarstellung.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a0a353f0-04f8-4d56-8285-5ce440aa88fb",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from geopy.geocoders import Nominatim\n",
"from geopy.exc import GeocoderTimedOut\n",
"import plotly.express as px"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85fd2f76-4a65-4310-a82f-d1e0b6168748",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"authors-csv.csv\", encoding=\"utf-8\")\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd119b8f-ff17-4055-8a0d-9c1b571c3b38",
"metadata": {},
"outputs": [],
"source": [
"# Initialisieren des Geolocators: \n",
"geolocator = Nominatim(user_agent=\"geoapiExercises\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce67491f-88de-44ae-8ebb-62707b78a477",
"metadata": {},
"outputs": [],
"source": [
"# Funktion, um Longitude und Latitude zu ermitteln:\n",
"def get_lat_long(place):\n",
" try:\n",
" location = geolocator.geocode(place)\n",
" if location:\n",
" return location.latitude, location.longitude\n",
" else:\n",
" return None, None\n",
" except GeocoderTimedOut:\n",
" return None, None"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec029f1f-20d2-4bd9-86ab-91aee865fe1c",
"metadata": {},
"outputs": [],
"source": [
"# Funktion auf der Spalte \"place\" ausführen, um neue Spalten \"lat\" und \"long\" zu ergänzen: \n",
"df[['lat', 'long']] = df['place'].apply(lambda x: pd.Series(get_lat_long(x)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "880d0dd3-7164-4091-985b-dca67334ec3e",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dcbaa61-c183-4f97-a0fd-168e5871ec9d",
"metadata": {},
"outputs": [],
"source": [
"df = df.dropna(subset=['lat', 'long'])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c122059-d170-4c5c-9134-317c684d30b1",
"metadata": {},
"outputs": [],
"source": [
"# Gruppieren bei \"place\" und zählen der Vorkommen der einzelnen Orte:\n",
"place_counts = df.groupby('place').size().reset_index(name='count')\n",
"place_counts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3952f9a7-8d8c-45bd-ac7a-eb6239ef51af",
"metadata": {},
"outputs": [],
"source": [
"# Zusammenführen der Zählungen mit dem ursprünglichen Dataframe in einem neuen Dataframe \"df_with_counts\":\n",
"df_with_counts = df.merge(place_counts, on='place')\n",
"df_with_counts"
]
},
{
"cell_type": "markdown",
"id": "0d8647d3-ac4c-4bd4-aa44-1829e2707484",
"metadata": {},
"source": [
"### Kartenvisualisierung erstellen:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64e8cadd-be66-4dff-a9f6-206b02b5a963",
"metadata": {},
"outputs": [],
"source": [
"# Karte erstellen:\n",
"fig = px.scatter_mapbox(df_with_counts, \n",
" lat=\"lat\", \n",
" lon=\"long\", \n",
" hover_name=\"place\", \n",
" hover_data=[\"count\"],\n",
" size=\"count\",\n",
" color=\"count\",\n",
" zoom=4,\n",
" height=600)\n",
"\n",
"# Update des Layouts\n",
"fig.update_layout(mapbox_style=\"open-street-map\")\n",
"fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
"\n",
"# Karte anzeigen: \n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83307d4d-f459-4064-a4ae-e08b6e6648da",
"metadata": {},
"outputs": [],
"source": [
"# Karte bei Bedarf als HTML-Datei speichern: \n",
"fig.write_html(\"places_map_plotly.html\")"
]
}
],
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 0e0ea07

Please sign in to comment.