简体   繁体   English

Pandas - get_dummies 与选定的集合

[英]Pandas - get_dummies with a selected set

With the following DataFrame:使用以下数据帧:

>>> df = pd.DataFrame(data={'category':['a','b','c'],'val':[1,2,3]})
>>> df
  category  val
0        a    1
1        b    2
2        c    3

I'm concatenating the produced dummy columns and droping the original column like so:我正在连接生成的虚拟列并删除原始列,如下所示:

>>> df = pd.concat([df, pd.get_dummies(df['category'], prefix='cat')], axis=1).drop(['category'], axis=1)
>>> df
   val  cat_a  cat_b  cat_c
0    1      1      0      0
1    2      0      1      0
2    3      0      0      1

I'm then adding another column for a future unknown value, like so:然后我为未来的未知值添加另一列,如下所示:

>>> df['cat_unkown'] = 0
>>> df
   val  cat_a  cat_b  cat_c  cat_unkown
0    1      1      0      0           0
1    2      0      1      0           0
2    3      0      0      1           0

Now I would like to get_dummies on a new DataFrame, but map it to the available columns, meaning: If a category column exists, use it otherwise set the cat_unkown to 1现在我想在新的 DataFrame 上 get_dummies,但将其映射到可用列,这意味着:如果存在类别列,则使用它,否则将 cat_unkown 设置为 1

for example for the following DataFrame:例如对于以下数据帧:

  category  val
0        a    1
1        b    2
2        d    3

The result would be:结果将是:

   val  cat_a  cat_b  cat_c  cat_unkonw
0    1      1      0      0           0
1    2      0      1      0           0
2    3      0      0      0           1

What would be an efficient way to do it?什么是一种有效的方法来做到这一点?

Update : Just elaborating a bit, In my real-world problem, I have the data frames after the get_dummies produced the results.更新:只是详细说明一下,在我的实际问题中,我在 get_dummies 产生结果后有数据帧。

I believe you need:我相信你需要:

df = pd.DataFrame(data={'category':['a','b','c'],'val':[1,2,3]})  
df = pd.concat([df, pd.get_dummies(df['category'], prefix='cat')], axis=1).drop(['category'], axis=1)  
df['cat_unkown'] = 0
print (df)
   val  cat_a  cat_b  cat_c  cat_unkown
0    1      1      0      0           0
1    2      0      1      0           0
2    3      0      0      1           0

df1 = pd.DataFrame(data={'category':['a','b','d'],'val':[1,2,3]})    
df1 = pd.concat([df1, pd.get_dummies(df1['category'], prefix='cat')], axis=1).drop(['category'], axis=1)  
print (df1)
   val  cat_a  cat_b  cat_d
0    1      1      0      0
1    2      0      1      0
2    3      0      0      1

#get all columns names without val
orig_cols = df.columns.difference(['val'])
print (orig_cols)
Index(['cat_a', 'cat_b', 'cat_c', 'cat_unkown'], dtype='object')

 #create dictionary with all columns from df1 which are not in df (also removed vals column)
dif = dict.fromkeys(df1.columns.difference(['val'] + orig_cols.tolist()), 'cat_unkown')
print (dif)
{'cat_d': 'cat_unkown'}

#rename columns and if-else for possible multiplied renamed columns
df3 = (df1.rename(columns=dif)
        .assign(cat_unkown = lambda x: x.pop('cat_unkown').max(axis=1) 
                             if isinstance(x['cat_unkown'], pd.DataFrame) 
                             else x.pop('cat_unkown'))
        .reindex(columns=orig_cols, fill_value=0)
        )

print (df3)
   cat_a  cat_b  cat_c  cat_unkown
0      1      0      0           0
1      0      1      0           0
2      0      0      0           1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM