Python Pandas:为源列的每个不同值创建一个新列(布尔输出作为列值) [英] Python Pandas: create a new column for each different value of a source column (with boolean output as column values)

查看:112
本文介绍了Python Pandas:为源列的每个不同值创建一个新列(布尔输出作为列值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据其内容将数据框的源列分成几列,然后以以下方式用布尔值1或0填充此新生成的列:

I am trying to split a source column of a dataframe in several columns based on its content, and then fill this newly generated columns with a boolean 1 or 0 in the following way:

原始数据框:

ID   source_column
A    value 1
B    NaN
C    value 2
D    value 3
E    value 2

生成以下输出:

ID   source_column    value 1    value 2    value 3
A    value 1          1          0          0
B    NaN              0          0          0
C    value 2          0          1          0
D    value 3          0          0          1
E    value 2          0          1          0

我考虑过手动创建每个不同的列,然后为每个列创建一个函数并.apply,用1或0填充新列,但这是非常无效的.

I thought about manually create each different column, and then with a function for each column and .apply, filling the newly column with a 1 or a 0, but this is highly ineffective.

有没有一种快速有效的方法?

Is there a quick and efficient way for this?

推荐答案

您可以使用pandas函数get_dummies,并将结果添加到df中,如下所示

You can use the pandas function get_dummies, and add the result to df as shown below

In [1]: col_names = df['source_column'].dropna().unique().tolist()

In [2]: df[col_names] = pd.get_dummies(df['source_column'])

In [3]: df
Out[3]: 
  ID source_column  value 1  value 2  value 3
0  A       value 1        1        0        0
1  B          NaN         0        0        0
2  C       value 2        0        1        0
3  D       value 3        0        0        1
4  E       value 2        0        1        0

这篇关于Python Pandas:为源列的每个不同值创建一个新列(布尔输出作为列值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆