使用另一个数据帧在数据帧中创建子列

2024-05-23

我对 python 和 pandas 很陌生。在这里,我有一个以下数据框。

did           features   offset   word   JAPE_feature  manual_feature 
0             200         0        aa      200          200 
0             200         11       bf      200          200
0             200         12       vf      100          100
0             100         13       rw      2200         2200
0             100         14       asd     2600         100 
0             2200        16       dsdd    2200         2200
0             2600        18       wd      2200         2600 
0             2600        20       wsw     2600         2600 
0             4600        21        sd     4600         4600

现在,我有一个数组,其中包含该 id 可以出现的所有特征值。

feat = [100,200,2200,2600,156,162,4600,100]

现在,我正在尝试创建一个数据框,它看起来像,

id                    Features 
           100   200   2200   2600  156   162    4600  100
0           0     1      0     0     0     0      0     0
1           0     1      0     0     0     0      0     0
2           0     1      0     0     0     0      0     0
3           0     1      0     0     0     0      0     0
4           1     0      0     0     0     0      0     0
5           1     0      0     0     0     0      0     0
7           0     0      1     0     0     0      0     0
8           0     0      0     1     0     0      0     0
9           0     0      0     1     0     0      0     0
10          0     0      0     0     0     0      1     0

所以,在进行比较时,

feature_manual
     1 
     1  
     0 
     0
     1
     1
     1
     1
     1

Here compairing the features and the manual_feature columns. if values are same then 1 or else 0. so 200 and 200 for 0 is same in both so 1 

所以,这是预期的输出。在这里,我尝试在新的 csv 中为该特征添加值 1,并为其他值添加 0。

So, it is by row by row.

因此,如果我们检查第一行的特征为 200,那么 200 处有 1,其他为 0。

谁能帮我这个 ?

我尝试过的是

mux = pd.MultiIndex.from_product([['features'],feat)
df = pd.DataFrame(data, columns=mux)

所以,这里创建子列但删除所有其他值。谁能帮我 ?


Use get_dummies http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.get_dummies.html with DataFrame.reindex http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html:

feat = [100,200,2200,2600,156,162,4600,100]
df = df.join(pd.get_dummies(df.pop('features')).reindex(feat, axis=1, fill_value=0))
print (df)
   id  100  200  2200  2600  156  162  4600  100
0   0    0    1     0     0    0    0     0    0
1   1    0    1     0     0    0    0     0    0
2   2    0    1     0     0    0    0     0    0
3   4    1    0     0     0    0    0     0    1
4   5    1    0     0     0    0    0     0    1
5   7    0    0     1     0    0    0     0    0
6   8    0    0     0     1    0    0     0    0
7   9    0    0     0     1    0    0     0    0
8  10    0    0     0     0    0    0     1    0

If need MultiIndex只能通过mux to reindex,还可以转换id列至index:

feat = [100,200,2200,2600,156,162,4600,100]
mux = pd.MultiIndex.from_product([['features'],feat])

df = pd.get_dummies(df.set_index('id')['features']).reindex(mux, axis=1, fill_value=0)
print (df)
   features                                   
       100  200  2200 2600 156  162  4600 100 
id                                            
0         0    0    0    0    0    0    0    0
1         0    0    0    0    0    0    0    0
2         0    0    0    0    0    0    0    0
4         0    0    0    0    0    0    0    0
5         0    0    0    0    0    0    0    0
7         0    0    0    0    0    0    0    0
8         0    0    0    0    0    0    0    0
9         0    0    0    0    0    0    0    0
10        0    0    0    0    0    0    0    0

EDIT:

cols = ['features', 'JAPE_feature', 'manual_feature']

df = pd.get_dummies(df, columns=cols)
df.columns = df.columns.str.rsplit('_',1, expand=True)
print (df)
  did offset  word features                    JAPE_feature                \
  NaN    NaN   NaN      100 200 2200 2600 4600          100 200 2200 2600   
0   0      0    aa        0   1    0    0    0            0   1    0    0   
1   0     11    bf        0   1    0    0    0            0   1    0    0   
2   0     12    vf        0   1    0    0    0            1   0    0    0   
3   0     13    rw        1   0    0    0    0            0   0    1    0   
4   0     14   asd        1   0    0    0    0            0   0    0    1   
5   0     16  dsdd        0   0    1    0    0            0   0    1    0   
6   0     18    wd        0   0    0    1    0            0   0    1    0   
7   0     20   wsw        0   0    0    1    0            0   0    0    1   
8   0     21    sd        0   0    0    0    1            0   0    0    0   

       manual_feature                     
  4600            100 200 2200 2600 4600  
0    0              0   1    0    0    0  
1    0              0   1    0    0    0  
2    0              1   0    0    0    0  
3    0              0   0    1    0    0  
4    0              1   0    0    0    0  
5    0              0   0    1    0    0  
6    0              0   0    0    1    0  
7    0              0   0    0    1    0  
8    1              0   0    0    0    1  

如果想避免缺失值MultIndex在没有列的列中MultiIndex:

cols = ['features', 'JAPE_feature', 'manual_feature']
df = df.set_index(df.columns.difference(cols).tolist())

df = pd.get_dummies(df, columns=cols)
df.columns = df.columns.str.rsplit('_',1, expand=True)
print (df)
                features                    JAPE_feature                     \
                     100 200 2200 2600 4600          100 200 2200 2600 4600   
did offset word                                                               
0   0      aa          0   1    0    0    0            0   1    0    0    0   
    11     bf          0   1    0    0    0            0   1    0    0    0   
    12     vf          0   1    0    0    0            1   0    0    0    0   
    13     rw          1   0    0    0    0            0   0    1    0    0   
    14     asd         1   0    0    0    0            0   0    0    1    0   
    16     dsdd        0   0    1    0    0            0   0    1    0    0   
    18     wd          0   0    0    1    0            0   0    1    0    0   
    20     wsw         0   0    0    1    0            0   0    0    1    0   
    21     sd          0   0    0    0    1            0   0    0    0    1   

                manual_feature                     
                           100 200 2200 2600 4600  
did offset word                                    
0   0      aa                0   1    0    0    0  
    11     bf                0   1    0    0    0  
    12     vf                1   0    0    0    0  
    13     rw                0   0    1    0    0  
    14     asd               1   0    0    0    0  
    16     dsdd              0   0    1    0    0  
    18     wd                0   0    0    1    0  
    20     wsw               0   0    0    1    0  
    21     sd                0   0    0    0    1 

EDIT:

如果想比较列表中的某些列manual_feature色谱柱使用DataFrame.eq http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.eq.html转换为整数:

cols = ['JAPE_feature', 'features']
df1 = df[cols].eq(df['manual_feature'], axis=0).astype(int)
print (df1)
   JAPE_feature  features
0             1         1
1             1         1
2             1         0
3             1         0
4             0         1
5             1         1
6             0         1
7             1         1
8             1         1 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用另一个数据帧在数据帧中创建子列 的相关文章

随机推荐