Examples¶
Titanic¶
import string
import pandas as pd
from pandas import CategoricalDtype
import pandas_lightning
Once you have imported, DataFrame.lambdas will be available to you.
Reading¶
>>> df = pd.read_csv("https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv")
Changing types¶
>>> df = df.cast(
... PassengerId="index",
... Name=str,
... Sex="category",
... Embarked="category",
... Pclass=[3, 2, 1],
... )
This is the same as
>>> df = df.set_index("PassengerId")
>>> df["Name"] = df["Name"].astype(str)
>>> df["Sex"] = df["Sex"].astype("category")
>>> df["Embarked"] = df["Embarked"].astype("category")
>>> df["Pclass"] = df["Pclass"].astype(CategoricalDtype([3, 2, 1], ordered=True)
Creating new features¶
>>> df = df.add_columns(
... Cabin=lambda s: s.str[0],
... HasCabinCode=("Cabin", lambda s: ~s.isna()),
... HasDep=(["SibSp", "Parch"], lambda s, t: (s+t) > 0),
... HasLetters=("Ticket", lambda s: s.str.startswith(tuple(string.ascii_letters)),
... )
which is the same as
>>> df["Cabin"] = df["Cabin"].str[0]
>>> df["HasCabinCode"] = ~df["Cabin"].isna()
>>> df["HasDep"] = df["SibSp"] + df["Parch"] > 0
>>> df["HasLetters"] = df["Ticket"].str.startswith(tuple(string.ascii_letters))