Python 用定性数据建立多元回归模型的矩阵

Python 用定性数据建立多元回归模型的矩阵,python,python-3.x,pandas,numpy,linear-regression,Python,Python 3.x,Pandas,Numpy,Linear Regression,我试图用定性数据建立一个多元回归模型 为了做到这一点,我需要构建一个新的数据框,该数据框基于唯一值创建一个包含列的新数据框,如果索引具有该值,则标记为1 例如: d = {'City': ['Tokyo','Tokyo','Lisbon','Tokyo','Madrid','Lisbon','Madrid','London','Tokyo','London','Tokyo'], 'Card': ['Visa','Visa','Visa','Master Card','Bitcoin'

我试图用定性数据建立一个多元回归模型

为了做到这一点,我需要构建一个新的数据框,该数据框基于唯一值创建一个包含列的新数据框,如果索引具有该值,则标记为1

例如:

d = {'City': ['Tokyo','Tokyo','Lisbon','Tokyo','Madrid','Lisbon','Madrid','London','Tokyo','London','Tokyo'], 
     'Card': ['Visa','Visa','Visa','Master Card','Bitcoin','Master Card','Bitcoin','Visa','Master Card','Visa','Bitcoin'],
     'Client Number':[1,2,3,4,5,6,7,8,9,10,11],
     }

d = pd.DataFrame(data=d).set_index('Client Number')
得到一个等于这个的结果


让我们试试
get\u dummies

df = pd.get_dummies(d,prefix='', prefix_sep='')
Out[202]: 
               Lisbon  London  Madrid  Tokyo  Bitcoin  Master Card  Visa
Client Number                                                           
1                   0       0       0      1        0            0     1
2                   0       0       0      1        0            0     1
3                   1       0       0      0        0            0     1
4                   0       0       0      1        0            1     0
5                   0       0       1      0        1            0     0
6                   1       0       0      0        0            1     0
7                   0       0       1      0        1            0     0
8                   0       1       0      0        0            0     1
9                   0       0       0      1        0            1     0
10                  0       1       0      0        0            0     1
11                  0       0       0      1        1            0     0