{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4) Daten laden und Dataframe Basisinfos\n",
"\n",
"In diesem Kapitel laden wir nun erstmals echte Daten. Es handelt sich dabei um einen recht überschaubaren Datensatz mit nur 5 Variablen (Spalten) und 294 Fällen (Zeilen). **Pandas** bietet die Möglichkeit, eine Vielzahl an unterschiedlichen Dateiformaten zu laden; in diesem Kapitel werden wir Daten aus einer CSV-Datei (.csv) sowie aus einer SPSS-Datei (.sav) einlesen."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.1) Import von CSV Datenfiles\n",
"\n",
"Lesen wir zunächsten ein CSV-File ein - dieses Format ist ein durchaus übliches bei vielen Programmen bzw. kann von den meisten (oder allen?) Statistiksoftwareprodukten verarbeitet werden. **Pandas** bietet für den Import die Funktion **read_csv()** an.\n",
"\n",
"[pandas.read_csv](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)\n",
"\n",
"**read_csv()** lässt sehr viele Einstellungen zu, ein Blick auf die verlinkte Seite mag sich lohnen."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"daten = pd.read_csv(\"C:\\\\Datenfiles\\\\daten.csv\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die Daten wurden geladen und sehen wie folgt aus (überschreitet die Anzahl der Zeilen einen bestimmten Grenzwert, so werden nur die ersten 5 sowie die letzten 5 Zeilen angezeigt):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" age | \n",
" wohnort | \n",
" volksmusik | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 50 | \n",
" 2 | \n",
" 2.666667 | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 57 | \n",
" 1 | \n",
" 1.000000 | \n",
" 3.333333 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 66 | \n",
" 3 | \n",
" 2.000000 | \n",
" 4.333333 | \n",
"
\n",
" \n",
" 3 | \n",
" 1 | \n",
" 50 | \n",
" 2 | \n",
" 2.333333 | \n",
" 2.666667 | \n",
"
\n",
" \n",
" 4 | \n",
" 1 | \n",
" 60 | \n",
" 3 | \n",
" 2.333333 | \n",
" 3.000000 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 289 | \n",
" 1 | \n",
" 24 | \n",
" 1 | \n",
" 5.000000 | \n",
" 2.000000 | \n",
"
\n",
" \n",
" 290 | \n",
" 2 | \n",
" 36 | \n",
" 1 | \n",
" 2.000000 | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 291 | \n",
" 2 | \n",
" 31 | \n",
" 1 | \n",
" 5.000000 | \n",
" 4.666667 | \n",
"
\n",
" \n",
" 292 | \n",
" 1 | \n",
" 37 | \n",
" 3 | \n",
" 5.000000 | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 293 | \n",
" 1 | \n",
" 23 | \n",
" 1 | \n",
" 4.333333 | \n",
" 3.333333 | \n",
"
\n",
" \n",
"
\n",
"
294 rows × 5 columns
\n",
"
"
],
"text/plain": [
" sex age wohnort volksmusik hardrock\n",
"0 1 50 2 2.666667 3.666667\n",
"1 1 57 1 1.000000 3.333333\n",
"2 2 66 3 2.000000 4.333333\n",
"3 1 50 2 2.333333 2.666667\n",
"4 1 60 3 2.333333 3.000000\n",
".. ... ... ... ... ...\n",
"289 1 24 1 5.000000 2.000000\n",
"290 2 36 1 2.000000 3.666667\n",
"291 2 31 1 5.000000 4.666667\n",
"292 1 37 3 5.000000 1.000000\n",
"293 1 23 1 4.333333 3.333333\n",
"\n",
"[294 rows x 5 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Erweitern (bzw. Einschränken) der Ansicht\n",
"\n",
"Man kann die Einstellungen für die Ansicht von Dataframes (bspw. angezeigte Zeilen- und Spaltenanzahl) selbst einstellen.\n",
"\n",
"[Options and settings](https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html)\n",
"\n",
"Bringen wird aber erstmal in Erfahrung, wie viele Zeilen und Spalten aktuell angezeigt werden."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.options.display.max_rows"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.options.display.max_columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aktuell werden also bis zu 60 Zeilen und 20 Spalten angezeigt. Im folgenden wird die Standardansicht mal auf wenige Zeilen und Spalten eingeschränkt. Umgekehrt kann sie natürlich auch erweitert werden."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.options.display.max_rows = 6\n",
"\n",
"pd.options.display.max_rows"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.options.display.max_columns = 3\n",
"\n",
"pd.options.display.max_columns"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" ... | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" ... | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" ... | \n",
" 3.333333 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" ... | \n",
" 4.333333 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 291 | \n",
" 2 | \n",
" ... | \n",
" 4.666667 | \n",
"
\n",
" \n",
" 292 | \n",
" 1 | \n",
" ... | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 293 | \n",
" 1 | \n",
" ... | \n",
" 3.333333 | \n",
"
\n",
" \n",
"
\n",
"
294 rows × 5 columns
\n",
"
"
],
"text/plain": [
" sex ... hardrock\n",
"0 1 ... 3.666667\n",
"1 1 ... 3.333333\n",
"2 2 ... 4.333333\n",
".. ... ... ...\n",
"291 2 ... 4.666667\n",
"292 1 ... 1.000000\n",
"293 1 ... 3.333333\n",
"\n",
"[294 rows x 5 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ändern wir die Ansicht aber nun wieder zu einem etwas sinnvollerem Umfang."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"pd.options.display.max_rows = 100\n",
"\n",
"pd.options.display.max_columns = 20"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Head vs Tail\n",
"\n",
"Längere Datensätze - wie der vorhin eingelesene - werden kaum stets zur Gänze angezeigt. Mit den Funktionen **head()** bzw. **tail()** kann man die ersten bzw. letzten 5 Zeilen (bzw. eine beliebige andere Anzahl, welche dann in der Klammer anzugeben ist: z.B. **head(3)**, **tail(12)**, usw.) anzeigen lassen.\n",
"\n",
"[pandas.DataFrame.head](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.head.html)\n",
"\n",
"[pandas.DataFrame.tail](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.tail.html)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" age | \n",
" wohnort | \n",
" volksmusik | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 50 | \n",
" 2 | \n",
" 2.666667 | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 57 | \n",
" 1 | \n",
" 1.000000 | \n",
" 3.333333 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 66 | \n",
" 3 | \n",
" 2.000000 | \n",
" 4.333333 | \n",
"
\n",
" \n",
" 3 | \n",
" 1 | \n",
" 50 | \n",
" 2 | \n",
" 2.333333 | \n",
" 2.666667 | \n",
"
\n",
" \n",
" 4 | \n",
" 1 | \n",
" 60 | \n",
" 3 | \n",
" 2.333333 | \n",
" 3.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sex age wohnort volksmusik hardrock\n",
"0 1 50 2 2.666667 3.666667\n",
"1 1 57 1 1.000000 3.333333\n",
"2 2 66 3 2.000000 4.333333\n",
"3 1 50 2 2.333333 2.666667\n",
"4 1 60 3 2.333333 3.000000"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" age | \n",
" wohnort | \n",
" volksmusik | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
" 289 | \n",
" 1 | \n",
" 24 | \n",
" 1 | \n",
" 5.000000 | \n",
" 2.000000 | \n",
"
\n",
" \n",
" 290 | \n",
" 2 | \n",
" 36 | \n",
" 1 | \n",
" 2.000000 | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 291 | \n",
" 2 | \n",
" 31 | \n",
" 1 | \n",
" 5.000000 | \n",
" 4.666667 | \n",
"
\n",
" \n",
" 292 | \n",
" 1 | \n",
" 37 | \n",
" 3 | \n",
" 5.000000 | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 293 | \n",
" 1 | \n",
" 23 | \n",
" 1 | \n",
" 4.333333 | \n",
" 3.333333 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sex age wohnort volksmusik hardrock\n",
"289 1 24 1 5.000000 2.000000\n",
"290 2 36 1 2.000000 3.666667\n",
"291 2 31 1 5.000000 4.666667\n",
"292 1 37 3 5.000000 1.000000\n",
"293 1 23 1 4.333333 3.333333"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.tail()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.2) Import von SPSS Datensätzen\n",
"\n",
"SPSS Datenfiles können mit **Pandas** (ab Version 0.25.0) grundsätzlich eingelesen werden (vgl. die Funktion **read_spss()**), jedoch liefert uns ein anderes Modul abseits von **Pandas** deutlich mehr Möglichkeiten für den Import von SPSS Daten. Es handelt sich dabei um das Modul **pyreadstat**, welches wir nun importieren.\n",
"\n",
"[pyreadstat](https://github.com/Roche/pyreadstat)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"import pyreadstat as prs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wir laden im Folgenden die Daten in ein Dataframe (welches wir *spssdaten* nennen) und importieren durch die Angabe von *meta* gleichzeitig div. Metadaten, die sich im SPSS Datenfile befinden (Metadaten sind bspw.: Variablennamen, Variablenlabels, usw.). Der Import der Metadaten ist nötig, da ansonsten (bspw. nur mit \"spssdaten = prs.read_sav('...')\") nur ein Tuple und kein Dataframe importiert würde."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"spssdaten, meta = prs.read_sav('C:\\\\Datenfiles\\\\daten.sav')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Das Dataframe sieht wie folgt aus (es handelt sich um den gleichen Datensatz, den wir schon vorhin aus einer CSV-Datei importiert haben)."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" age | \n",
" wohnort | \n",
" volksmusik | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.0 | \n",
" 50.0 | \n",
" 2.0 | \n",
" 2.666667 | \n",
" 3.666667 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.0 | \n",
" 57.0 | \n",
" 1.0 | \n",
" 1.000000 | \n",
" 3.333333 | \n",
"
\n",
" \n",
" 2 | \n",
" 2.0 | \n",
" 66.0 | \n",
" 3.0 | \n",
" 2.000000 | \n",
" 4.333333 | \n",
"
\n",
" \n",
" 3 | \n",
" 1.0 | \n",
" 50.0 | \n",
" 2.0 | \n",
" 2.333333 | \n",
" 2.666667 | \n",
"
\n",
" \n",
" 4 | \n",
" 1.0 | \n",
" 60.0 | \n",
" 3.0 | \n",
" 2.333333 | \n",
" 3.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sex age wohnort volksmusik hardrock\n",
"0 1.0 50.0 2.0 2.666667 3.666667\n",
"1 1.0 57.0 1.0 1.000000 3.333333\n",
"2 2.0 66.0 3.0 2.000000 4.333333\n",
"3 1.0 50.0 2.0 2.333333 2.666667\n",
"4 1.0 60.0 3.0 2.333333 3.000000"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spssdaten.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nun wollen wir sehen, welche Metadaten im SPSS-Datenfile enthalten sind. Die Spaltennamen wahrscheinlich..."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sex', 'age', 'wohnort', 'volksmusik', 'hardrock']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.column_names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... und auch die Variablenlabels (im Gegensatz zu den meist kurzen Variablennamen in SPSS handelt es sich dabei um meist ausführlichere Bezeichnungen der Variablen). Die Variablen 4 und 5 weisen leider keine Variablenlabels auf."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Geschlecht', 'Alter', 'Wohnumgebung', None, None]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.column_labels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Es gibt auch die Möglichkeit, die Variablennamen mit den dazugehörigen Variablenlabels ausgeben zu lassen."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sex': 'Geschlecht',\n",
" 'age': 'Alter',\n",
" 'wohnort': 'Wohnumgebung',\n",
" 'volksmusik': None,\n",
" 'hardrock': None}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.column_names_to_labels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die Anzahl der Zeilen und Spalten ist auch in den Metadaten enthalten."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"294"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.number_rows"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.number_columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ein Filelabel steht nicht zur Verfügung, die Zeichenkodierung ist UTF-8."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"meta.file_label # steht offenbar keines zur Verfügung, daher wird nichts angezeigt..."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'UTF-8'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.file_encoding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Und schließlich gibt es noch eine sehr brauchbare Information, die wir mit einem Import aus CSV-Dateien nicht hätten. In SPSS kann man den Werten bestimmte Labels zuweisen - diese sind in den importierten Metadaten enhalten und helfen uns für alle nachfolgenden Auswertungen, da wir nun wissen, was die Zahlen bedeuten. Für die Variable *age* waren keine Labels enthalten und werden auch nicht benötigt - es handelt sich um das Alter in Jahren. Für die Variablen *volksmusik* und *hardrock* waren ebenso keine Labels enthalten - bei beiden Variablen handelt es sich um Präferenzskalen von 1 (diese Musikrichtung höre ich sehr gerne) bis 5 (diese Musikrichtung höre ich überhaupt nicht gerne)."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sex': {1.0: 'weiblich', 2.0: 'männlich'},\n",
" 'wohnort': {1.0: 'In ländlicher Umgebung',\n",
" 2.0: 'In kleinstädtischer Umgebung',\n",
" 3.0: 'In großstädtischer Umgebung (z.B. Graz, Wien)'}}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meta.variable_value_labels # Wertelabels anzeigen (welche in SPSS definiert wurden)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.3) Import von Daten aus der Windows Zwischenablage\n",
"\n",
"Auch aus der Windows Zwischenablage können Daten eingelesen werden. Z.B. könnte man in Excel einige Zeilen und Spalten markieren, diese kopieren und mit untenstehender Funktion **read_clipboard()** anschließend als Dataframe importieren. Untenstehende Daten wurden in einem Excel-Datenblatt eingegeben und auf dem beschriebenen Weg eingelesen. Der Zeilenindex wird dabei - wie bekannt - automatisch erstellt, d.h. aus Excel stammen nur die drei Spalten *Hausnr.*, *Hund* und *Katze*.\n",
"\n",
"[pandas.read_clipboard](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_clipboard.html)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Hausnr. | \n",
" Hund | \n",
" Katze | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" Bauxi | \n",
" Carlo | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" Rex | \n",
" Miez | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" Bello | \n",
" Pezi | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Hausnr. Hund Katze\n",
"0 1 Bauxi Carlo\n",
"1 2 Rex Miez\n",
"2 3 Bello Pezi"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_clipboard()\n",
"\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.4) Grundlagen zu Dataframes\n",
"\n",
"Sehen wir uns nun noch ein paar Eckdaten zum ursprünglich geladenen Dataframe *daten* an. **Pandas** bietet dafür einige Möglichkeiten an. Einen Überblick über die Spalten(Variablen)namen im Dataframe, die Datentypen, die Anzahl der Zeilen und Spalten und den Speicherplatzbedarf des Files bietet die Funktion **info()**.\n",
"\n",
"[DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html)\n",
"\n",
"[DataFrame Constructor, Attributes, usw.](https://pandas.pydata.org/pandas-docs/stable/reference/frame.html#constructor)\n",
"\n",
"[DataFrame.shape](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.shape.html)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 294 entries, 0 to 293\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 sex 294 non-null int64 \n",
" 1 age 294 non-null int64 \n",
" 2 wohnort 294 non-null int64 \n",
" 3 volksmusik 294 non-null float64\n",
" 4 hardrock 294 non-null float64\n",
"dtypes: float64(2), int64(3)\n",
"memory usage: 11.6 KB\n"
]
}
],
"source": [
"daten.info()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Das Attribut **shape** gibt die Anzahl der Zeilen und Spalten an."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(294, 5)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.shape # Anzahl der Zeilen und Spalten (nur Variablen, d.h. ohne den automatisch vergebenen Index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**size** gibt die Anzahl an Zellen (Zeilen x Spalten) an."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1470"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.size # Anzahl der Elemente (Zellen) im Dataframe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**axes** liefert Informationen über den Zeilen- und den Spaltenindex."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[RangeIndex(start=0, stop=294, step=1),\n",
" Index(['sex', 'age', 'wohnort', 'volksmusik', 'hardrock'], dtype='object')]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.axes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ausschließlich Informationen zum Zeilenindex liefert **index**."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RangeIndex(start=0, stop=294, step=1)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.index"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Um welche Datentypen es sich bei den Variablen im Dataframe handelt, erfährt man bspw. mit **dtypes**."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sex int64\n",
"age int64\n",
"wohnort int64\n",
"volksmusik float64\n",
"hardrock float64\n",
"dtype: object"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die Funktion **nunique()** schließlich gibt für jede Spalte (Variable) an, wieviele unterschiedliche Werte darin enthalten sind. Bsp. enthält die Variable *sex* nur 2 unterschiedliche Werte (1 bzw. 2 für weiblich bzw. männlich), während die Variable *age* (Alter in Jahren) bereits 53 verschiedene Werte enthält."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sex 2\n",
"age 53\n",
"wohnort 3\n",
"volksmusik 14\n",
"hardrock 16\n",
"dtype: int64"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.nunique() # Anzahl individueller (unterschiedlicher) Werte pro Spalte/Variable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.5) Liste der Spalten(Variablen)namen anzeigen\n",
"\n",
"Abschließend sei noch gezeigt, auf welchen unterschiedlichen Wegen man sich die Spalten(Variablen)namen anzeigen lassen kann.\n",
"\n",
"[How to get column names in Pandas dataframe](http://www.datasciencemadesimple.com/get-list-column-headers-column-name-python-pandas/)\n",
"\n",
"[Get the list of column headers or column name in python pandas](https://www.geeksforgeeks.org/how-to-get-column-names-in-pandas-dataframe/)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mit dem Attribut **columns** funktioniert es ganz gut."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['sex', 'age', 'wohnort', 'volksmusik', 'hardrock'], dtype='object')"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Auch die bereits bekannte Funktion **head()** zeigt natürlich immer die Spaltennamen an."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sex | \n",
" age | \n",
" wohnort | \n",
" volksmusik | \n",
" hardrock | \n",
"
\n",
" \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [sex, age, wohnort, volksmusik, hardrock]\n",
"Index: []"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"daten.head(0) # '0', da wir nur die Spaltennamen wollten..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Folgende kurze **for**-Schleife gibt alle Spaltennamen in der Reihenfolge ihres Auftretens im Dataframe aus."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sex\n",
"age\n",
"wohnort\n",
"volksmusik\n",
"hardrock\n"
]
}
],
"source": [
"for col in daten.columns: \n",
" print(col)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ähnliches, wenn auch anders dargestellt, bewirkt die **list()** Funktion."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sex', 'age', 'wohnort', 'volksmusik', 'hardrock']"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(daten.columns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Das funktioniert auch ohne die Angabe von **columns**."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sex', 'age', 'wohnort', 'volksmusik', 'hardrock']"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(daten)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Schließlich gibt die **sorted()** Funktion die Spaltennamen in alphabetischer Reihenfolge aus."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['age', 'hardrock', 'sex', 'volksmusik', 'wohnort']"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(daten.columns) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wiederum funktioniert dies auch ohne die Angabe von **columns**."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['age', 'hardrock', 'sex', 'volksmusik', 'wohnort']"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(daten) "
]
}
],
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}