Pandas mit NumPy kombinieren
Pandas basiert auf NumPy, und die beiden sind eng integriert. Das Verständnis ihrer Interaktion kann die Datenverarbeitung und das wissenschaftliche Rechnen effizienter machen.
Interkonvertierung
Konvertierung zwischen DataFrame/Series und NumPy
Beispiel
import pandas as pd
import numpy as np
# DataFrame in NumPy-Array konvertieren
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
arr = df.to_numpy()
print("DataFrame zu Array:")
print(arr)
print(f"Typ: {type(arr)}")
print()
# Series in Array umwandeln
s = pd.Series([1, 2, 3])
arr = s.values # Oder s.to_numpy()
print("Series in Array:")
print(arr)
print()
# NumPy in DataFrame umwandeln
arr = np.array([[1, 2], [3, 4], [5, 6]])
df = pd.DataFrame(arr, columns=["A", "B"])
print("Array zu DataFrame:")
print(df)
import numpy as np
# DataFrame in NumPy-Array konvertieren
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
arr = df.to_numpy()
print("DataFrame zu Array:")
print(arr)
print(f"Typ: {type(arr)}")
print()
# Series in Array umwandeln
s = pd.Series([1, 2, 3])
arr = s.values # Oder s.to_numpy()
print("Series in Array:")
print(arr)
print()
# NumPy in DataFrame umwandeln
arr = np.array([[1, 2], [3, 4], [5, 6]])
df = pd.DataFrame(arr, columns=["A", "B"])
print("Array zu DataFrame:")
print(df)
Verwendung von NumPy-Funktionen in Pandas
Beispiel
import pandas as pd
import numpy as np
s = pd.Series([1, 2, 3, 4, 5])
# NumPy-Funktionen verwenden
print("Absolutwert:", np.abs(s))
print("Quadratwurzel:", np.sqrt(s))
print("Exponential:", np.exp(s))
print("Logarithmus:", np.log(s))
print()
# Bedingte Filterung
print("Werte größer als 3:")
print(s[s > 3])
import numpy as np
s = pd.Series([1, 2, 3, 4, 5])
# NumPy-Funktionen verwenden
print("Absolutwert:", np.abs(s))
print("Quadratwurzel:", np.sqrt(s))
print("Exponential:", np.exp(s))
print("Logarithmus:", np.log(s))
print()
# Bedingte Filterung
print("Werte größer als 3:")
print(s[s > 3])
Vektorisierte Operationen
Beispiel
import pandas as pd
import numpy as np
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
# Vektorisierte Operationen
print("A + B:", (df["A"] + df["B"]).tolist())
print("A * B:", (df["A"] * df["B"]).tolist())
print("A > B:", (df["A"] > df["B"]).tolist())
print()
# Verwende apply für elementweise Berechnung
result = df.apply(lambda x: np.multiply(x, 2))
print("Jedes Element * 2:")
print(result)
import numpy as np
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
# Vektorisierte Operationen
print("A + B:", (df["A"] + df["B"]).tolist())
print("A * B:", (df["A"] * df["B"]).tolist())
print("A > B:", (df["A"] > df["B"]).tolist())
print()
# Verwende apply für elementweise Berechnung
result = df.apply(lambda x: np.multiply(x, 2))
print("Jedes Element * 2:")
print(result)
Numerische Berechnungstechniken
Beispiel
import pandas as pd
import numpy as np
df = pd.DataFrame({
"A": [1, 2, 3, 4, 5],
"B": [10, 20, 30, 40, 50]
})
# Berechne die prozentuale Änderung von A relativ zu B
df["Rate of change"] = np.divide(df["A"], df["B"], where=df["B"] != 0) * 100
print("Prozentuale Änderung:")
print(df)
print()
# Verwende np.where für bedingte Zuweisung
df["Bezeichnung"] = np.where(df["A"] > 3, "Groß", "Klein")
print("Bedingte Bezeichnung:")
print(df)
import numpy as np
df = pd.DataFrame({
"A": [1, 2, 3, 4, 5],
"B": [10, 20, 30, 40, 50]
})
# Berechne die prozentuale Änderung von A relativ zu B
df["Rate of change"] = np.divide(df["A"], df["B"], where=df["B"] != 0) * 100
print("Prozentuale Änderung:")
print(df)
print()
# Verwende np.where für bedingte Zuweisung
df["Bezeichnung"] = np.where(df["A"] > 3, "Groß", "Klein")
print("Bedingte Bezeichnung:")
print(df)
Statistische Berechnung
Beispiel
import pandas as pd
import numpy as np
s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Statistische Funktionen in NumPy
print(f"Mittelwert: {np.mean(s):.2f}")
print(f"Standardabweichung: {np.std(s):.2f}")
print(f"Maximum: {np.max(s)}")
print(f"Minimum: {np.min(s)}")
print()
# Median
print(f"Median: {np.median(s)}")
import numpy as np
s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Statistische Funktionen in NumPy
print(f"Mittelwert: {np.mean(s):.2f}")
print(f"Standardabweichung: {np.std(s):.2f}")
print(f"Maximum: {np.max(s)}")
print(f"Minimum: {np.min(s)}")
print()
# Median
print(f"Median: {np.median(s)}")
Andere ErweiterungenDie tiefe Integration von Pandas und NumPy macht die Verarbeitung komplexer Daten einfach und effizient.