Quickstart
Assign Columns
import pandas as pd
from colassigner import ColAssigner
class Cols(ColAssigner):
def col1(self, df):
return df.iloc[:, 0] * 2
def col2(self, df):
return "added-another"
df = pd.DataFrame({"a": [1, 2, 3]}).pipe(Cols())
df
| a | col_1 | col_2 | |
|---|---|---|---|
| 0 | 1 | 2 | added-another |
| 1 | 2 | 4 | added-another |
| 2 | 3 | 6 | added-another |
df.loc[:, Cols.col2]
0 added-another
1 added-another
2 added-another
Name: col_2, dtype: object
Access Columns
while also documenting datatypes
from colassigner import ColAccessor
class Cols(ColAccessor):
x = int
y = float
df = pd.DataFrame({Cols.x: [1, 2, 3], Cols.y: [0.3, 0.1, 0.9]})
df
| x | y | |
|---|---|---|
| 0 | 1 | 0.3 |
| 1 | 2 | 0.1 |
| 2 | 3 | 0.9 |
df.loc[:, Cols.y]
0 0.3
1 0.1
2 0.9
Name: y, dtype: float64