对 pandas 中的一列字符串进行分解 [英] Factorize a column of strings in pandas

查看:33
本文介绍了对 pandas 中的一列字符串进行分解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所说,我有一个数据框 df_original,它非常大,但看起来像:

As the question says, I have a data frame df_original which is quite large but looks like:

        ID    Count   Column 2   Column 3  Column 4
RowX    1      234.     255.       yes.      452
RowY    1      123.     135.       no.       342
RowW    1      234.     235.       yes.      645
RowJ    1      123.     115.       no.       342
RowA    1      234.     285.       yes.      233
RowR    1      123.     165.       no.       342
RowX    2      234.     255.       yes.      234
RowY    2      123.     135.       yes.      342
RowW    2      234.     235.       yes.      233
RowJ    2      123.     115.       yes.      342
RowA    2      234.     285.       yes.      312
RowR    2      123.     165.       no.       342
.
.
.
RowX    1233   234.     255.       yes.      133
RowY    1233   123.     135.       no.       342
RowW    1233   234.     235.       no.       253
RowJ    1233   123.     115.       yes.      342
RowA    1233   234.     285.       yes.      645
RowR    1233   123.     165.       no.       342

我正在尝试删除文本数据并将其替换为预定义的等效数字.例如,在这种情况下,我想用 1 替换 Column3yesno 值或 0 分别.有没有办法做到这一点,而无需我手动进入并更改值?

I am trying to get rid of the text data and replace it with a predefined numerical equivalent. For example, in this case, I'd like to replace Column3's yes or no values with 1 or 0 respectively. Is there a way to do this without me having to manually go in and alter the values?

推荐答案

series

RowX    yes
RowY     no
RowW    yes
RowJ     no
RowA    yes
RowR     no
RowX    yes
RowY    yes
RowW    yes
RowJ    yes
RowA    yes
RowR     no
Name: Column 3, dtype: object

pd.factorize

1 - series.factorize()[0]
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0])
    


np.where

np.where(series == 'yes', 1, 0)
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0])


pd.Categorical/astype('category')

pd.Categorical(series).codes
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0], dtype=int8)

series.astype('category').cat.codes

RowX    1
RowY    0
RowW    1
RowJ    0
RowA    1
RowR    0
RowX    1
RowY    1
RowW    1
RowJ    1
RowA    1
RowR    0
dtype: int8


pd.Series.replace

series.replace({'yes' : 1, 'no' : 0})
 
RowX    1
RowY    0
RowW    1
RowJ    0
RowA    1
RowR    0
RowX    1
RowY    1
RowW    1
RowJ    1
RowA    1
RowR    0
Name: Column 3, dtype: int64

上述内容的一个有趣的通用版本:

A fun, generalised version of the above:

series.replace({r'^(?!yes).*$' : 0}, regex=True).astype(bool).astype(int)

RowX    1
RowY    0
RowW    1
RowJ    0
RowA    1
RowR    0
RowX    1
RowY    1
RowW    1
RowJ    1
RowA    1
RowR    0
Name: Column 3, dtype: int64

任何不是是"的都是0.

这篇关于对 pandas 中的一列字符串进行分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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