Pakete

Entwicklung mit Python für Fortgeschrittene

Jasmin Ludolf

Senior Data Science Content Developer

Module sind Python-Dateien

  • Modul = Python-Datei

  • Jeder kann eine Python-Datei erstellen!

Code-Datei auf einem Laptop

Entwicklung mit Python für Fortgeschrittene

Pakete

  • Eine Sammlung von Modulen = Paket
    • Auch Bibliothek genannt
  • Für alle zugänglich und kostenlos
  • Wird über PyPi heruntergeladen
  • Kann anschließend importiert und wie Module genutzt werden.

Großer Karton

1 https://pypi.org/
Entwicklung mit Python für Fortgeschrittene

Ein Paket installieren

  • Terminal / Eingabeaufforderung

    python3 -m pip install <package_name>
    
  • python3 führt Python-Code vom Terminal aus

  • pip: Bevorzugtes Installationsprogramm

Codierungsterminal

Entwicklung mit Python für Fortgeschrittene

Ein Paket installieren

 

python3 -m pip install pandas

pandas logo

$$

  • Paket für Datenbearbeitung und -analyse
Entwicklung mit Python für Fortgeschrittene

Importieren mit einem Alias

# Import pandas
import pandas
  • Benutze einen Alias, kürzeren Code zu schreiben.
# Import pandas using an alias
import pandas as pd
Entwicklung mit Python für Fortgeschrittene

Erstellen eines DataFrame

# Sales dictionary
sales = {"user_id": ["KM37", "PR19", "YU88"],
         "order_value": [197.75, 208.21, 134.99]}

# Convert to a pandas DataFrame sales_df = pd.DataFrame(sales)
print(sales_df)
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
Entwicklung mit Python für Fortgeschrittene

Lesen einer CSV-Datei

# Reading in a CSV file in our current directory
sales_df = pd.read_csv("sales.csv")

# Checking the data type print(type(sales_df))
pandas.core.frame.DataFrame
Entwicklung mit Python für Fortgeschrittene

Vorschau einer Datei

# DataFrame method to preview the first five rows
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
Entwicklung mit Python für Fortgeschrittene

Die Datei-Informationen prüfen

# Checking the file info
print(sales_df.info())
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column       Non-Null Count  Dtype  
<hr />  ------       --------------  -----  
 0   user_id      3 non-null      object 
 1   order_value  3 non-null      float64
dtypes: float64(1), object(1)
memory usage: 180.0+ bytes
Entwicklung mit Python für Fortgeschrittene

Funktionen versus Methoden

# This is a built-in function
print(sum([1, 2 ,3, 4, 5]))
15
  • Funktion = Code, um eine Aufgabe zu erledigen
# This is a pandas function
sales_df = pd.DataFrame(sales)
  • .head() funktioniert nur mit pandas-dataframes.
# This is a method
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
  • Methode = eine Funktion, die spezifisch für einen bestimmten Datentyp ist.
Entwicklung mit Python für Fortgeschrittene

Lass uns üben!

Entwicklung mit Python für Fortgeschrittene

Preparing Video For Download...