从同一列中的df中将同一行中的值拆分为同一值?

 收藏

我的数据框有一列,其中包含一些值,这些值可以出现在所有行中,但不是必需的。 我想将此值拆分为不同的行,如果该值出现在该列中,则将其放在1

我正在使用python 3并且df是:

data = {'Amenities':[{'TV', 'Wifi'}, {'TV',"Cable TV",'Internet','Wifi'},
                     {'Internet','Wifi'}]}  
df2 = pd.DataFrame(data) 
df2

    Amenities
0   {TV, Wifi}
1   {TV, Cable TV, Wifi, Internet}
2   {Wifi, Internet}

我想以这种方式查看表格:

data = {'Amenities':[{'TV', 'Wifi'}, {'TV',"Cable TV",'Internet','Wifi'},
                     {'Internet','Wifi'}],
        'TV':[1,1,0],
        'Cable TV':[0,1,0],
        'Wifi':[1,1,1],
        'Internet':[0,1,1],
       }  
df2 = pd.DataFrame(data) 
df2

    Amenities   TV  Cable TV    Wifi    Internet
0   {TV, Wifi}  1   0   1   0
1   {TV, Cable TV, Wifi, Internet}  1   1   1   1
2   {Wifi, Internet}    0   0   1   1
回复
  • xeos 回复

    In your case using get_dummies then concat

    s=pd.get_dummies(df2.Amenities.apply(list).apply(pd.Series),prefix = '',prefix_sep = '').sum(level=0,axis=1)
    s
    Out[66]: 
       Cable TV  Internet  TV  Wifi
    0         0         0   1     1
    1         1         1   1     1
    2         0         1   0     1
    df=pd.concat([df2,s],axis=1)
    df
    Out[68]: 
                            Amenities  Cable TV  Internet  TV  Wifi
    0                      {TV, Wifi}         0         0   1     1
    1  {Cable TV, Internet, Wifi, TV}         1         1   1     1
    2                {Internet, Wifi}         0         1   0     1