简体   繁体   English

Python - Pandas - DataFrame - 根据条件将单个列分解为多个布尔列

[英]Python - Pandas - DataFrame - Explode single column into multiple boolean columns based on conditions

Good morning chaps, 早上好,

Any pythonic way to explode a dataframe column into multiple columns with boolean flags, based on some condition (str.contains in this case)? 基于某些条件(在这种情况下为str.contains),使用布尔标志将数据帧列分解为多个列的任何pythonic方法?

Let's say I have this: 假设我有这个:

Position Letter 
1        a      
2        b      
3        c      
4        b      
5        b

And I'd like to achieve this: 我想实现这个目标:

Position Letter is_a     is_b    is_C
1        a      TRUE     FALSE   FALSE
2        b      FALSE    TRUE    FALSE
3        c      FALSE    FALSE   TRUE
4        b      FALSE    TRUE    FALSE
5        b      FALSE    TRUE    FALSE 

Can do with a loop through 'abc' and explicitly creating new df columns, but wondering if some built-in method already exists in pandas. 可以通过'abc'循环并显式创建新的df列,但想知道pandas中是否已存在某些内置方法。 Number of possible values, and hence number of new columns is variable. 可能的值的数量,因此新列的数量是可变的。

Thanks and regards. 感谢致敬。

use Series.str.get_dummies() : 使用Series.str.get_dummies()

In [31]: df.join(df.Letter.str.get_dummies())
Out[31]:
   Position Letter  a  b  c
0         1      a  1  0  0
1         2      b  0  1  0
2         3      c  0  0  1
3         4      b  0  1  0
4         5      b  0  1  0

or 要么

In [32]: df.join(df.Letter.str.get_dummies().astype(bool))
Out[32]:
   Position Letter      a      b      c
0         1      a   True  False  False
1         2      b  False   True  False
2         3      c  False  False   True
3         4      b  False   True  False
4         5      b  False   True  False

暂无
暂无

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

相关问题 根据多个条件在 pandas dataframe 中创建多个 boolean 列 - Create multiple boolean columns in pandas dataframe based on multiple conditions 基于布尔条件的 Pandas 数据框中的新列 - New column in Pandas dataframe based on boolean conditions 根据其他行和列的多个条件在数据框中创建新列? 包括空行? - 蟒蛇/熊猫 - Creating a new column in dataframe based on multiple conditions from other rows and columns? Including rows that are null? - Python/Pandas 根据熊猫数据框中的条件将单元格拆分/分解为多行 - Split/explode cells into multiple rows based on conditions in pandas dataframe Python:根据Python中的多个条件更改pandas DataFrame列中的值 - Python: Change values in a pandas DataFrame column based on multiple conditions in Python 如何基于来自熊猫中其他数据框的多个条件在数据框中创建新的布尔列 - How to create a new boolean column in a dataframe based on multiple conditions from other dataframe in pandas 根据多个其他列的条件新建 Python DataFrame 列 - Create new Python DataFrame column based on conditions of multiple other columns 在多个布尔列中拆分pandas dataframe列 - splitting pandas dataframe column in multiple boolean columns Pandas - 展开 pandas 中的多个列并根据展开的列分配值 - Pandas - Explode multiple columns in pandas and assign value based on the exploded column 根据Pandas DataFrame中单个列中的值创建多个列 - Create multiple columns based on values in single column in Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM