【问题标题】:Create a sub columns in the dataframe using a another dataframe使用另一个数据框在数据框中创建子列
【发布时间】:2019-11-22 09:21:16
【问题描述】:

我是 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)

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 实际上我尝试使用 for 循环并创建子列.. 但它不起作用。
  • 请包含该代码并显示您从中获得的输出,以便人们更容易回答您的问题
  • 添加了我试过的请检查

标签: python python-3.x pandas numpy


【解决方案1】:

get_dummiesDataFrame.reindex 一起使用:

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

如果需要MultiIndex,只需将mux 传递给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

编辑:

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  

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

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 

编辑:

如果想通过manual_feature 列比较列表中的某些列,请使用DataFrame.eq 并转换为整数:

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 

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @GaneshKaspate - 但只是显示问题,check this
  • 我的错误我编辑了答案,而不是在问题中添加该信息,对此感到抱歉
  • @GaneshKaspate - 所以你想要显示值?因为如果写入文件,则 MultiIndex 第一级的值会正确重复。
  • @GaneshKaspate - 好的,所以请使用来自with pd.option_context('display.multi_sparse', False): print (df1)link 解决方案
【解决方案2】:

不太花哨的解决方案,但可能更容易理解:

首先,将决定您在每一行中选择哪个功能的功能放入一个名为 list_features 的列表中。

然后:

# List all the features possible and create an empty df
feat = [100,200,2200,2600,156,162,4600,100]
df_final= pd.DataFrame({x:[] for x in feat})

# Fill the df little by little
for x in list_features:
    df_final = df_final.append({y:1 if x==y else 0 for y in feat }, ignore_index=True)

【讨论】:

    【解决方案3】:

    这些类型的问题可以通过多种方式解决。但在这里我使用简单的方法来解决它。使用这些功能列表创建 df 作为列名,并使用一些比较逻辑来使用 0 和 1 更新 df。您可以使用其他一些逻辑来避免使用 for 循环。

    import pandas as pd
    
    data = {'id':[0,1,2,3,4,5,7,8,9,10],
    'features':[200, 200, 200, 200, 100, 100, 2200, 2600, 2600, 4600]}
    
    df1 = pd.DataFrame(data)
    
    features_list = [100,200,2200,2600,156,162,4600]
    id_list = df1.id.to_list()
    
    df2 = pd.DataFrame(columns=features_list)
    list2 = list()
    
    for i in id_list:
        list1 = list()
        for k in df2.columns:
            if df1[df1.id == i].features.iloc[0] == k:
                list1.append(1)
            else:
                list1.append(0)
        list2.append(list1)
    
    for i in range (0,len(list2)):
        df2.loc[i] = list2[i]
    
    df2.insert(0, "id", id_list)   
    
    >>>(df2)
       id 100 200 2200 2600 156 162 4600
    0   0   0   1    0    0   0   0    0
    1   1   0   1    0    0   0   0    0
    2   2   0   1    0    0   0   0    0
    3   3   0   1    0    0   0   0    0
    4   4   1   0    0    0   0   0    0
    5   5   1   0    0    0   0   0    0
    6   7   0   0    1    0   0   0    0
    7   8   0   0    0    1   0   0    0
    8   9   0   0    0    1   0   0    0
    9  10   0   0    0    0   0   0    1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多