使用dmatrices函数:

import numpy as np
import pandas as pddataframe = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=['col1', 'col2', 'col3', 'prep']
)
# 使用Patsy公示语法后的 X 与 Y 可以通过这个看:
from patsy import dmatrices
y, X = dmatrices('prep ~ col1 + col2 + col3', data=dataframe, return_type='dataframe')