有条件地改变列
Mutate column conditionally
我是一名试图进入 Python 的 R 程序员。在 R 中,当我想有条件地改变列时,我使用:
col = dplyr::mutate(col, ifelse(condition, if_true(x), if_false(x))
在Python中,如何有条件地改变列值?这是我的最小可重现示例:
def act(cntnt):
def do_thing(cntnt):
return(cntnt + "has it")
def do_other_thing(cntnt):
return(cntnt + "nope")
has_abc = cntnt.str.contains.contains("abc")
if has_abc == T:
cntnt[has_abc].apply(do_thing)
else:
cntnt[has_abc].apply(do_other_thing)
您可以使用条件(及其否定)进行逻辑索引:
has_abc = cntnt.str.contains("abc")
cntnt[ has_abc].apply(do_thing)
cntnt[~has_abc].apply(do_other_thing)
我想你要找的是 assign
,它本质上是 pandas 等同于 dplyr
中的 mutate
。您的条件语句可以使用列表理解或使用矢量化方法编写(见下文)。
以数据框为例,我们称它为df
:
> df
a
1 0.50212013
2 1.01959213
3 -1.32490344
4 -0.82133375
5 0.23010548
6 -0.64410737
7 -0.46565442
8 -0.08943858
9 0.11489957
10 -0.21628132
R
/ dplyr
:
在R
中,您可以使用mutate
和ifelse
来根据条件创建一个列(在这个例子中,当列a时它将是'pos'
大于 0
):
df = dplyr::mutate(df, col = ifelse(df$a > 0, 'pos', 'neg'))
结果df
:
> df
a col
1 0.50212013 pos
2 1.01959213 pos
3 -1.32490344 neg
4 -0.82133375 neg
5 0.23010548 pos
6 -0.64410737 neg
7 -0.46565442 neg
8 -0.08943858 neg
9 0.11489957 pos
10 -0.21628132 neg
Python
/ Pandas
在 pandas
中,使用带有列表理解的 assign
:
df = df.assign(col = ['pos' if a > 0 else 'neg' for a in df['a']])
结果df
:
>>> df
a col
0 0.502120 pos
1 1.019592 pos
2 -1.324903 neg
3 -0.821334 neg
4 0.230105 pos
5 -0.644107 neg
6 -0.465654 neg
7 -0.089439 neg
8 0.114900 pos
9 -0.216281 neg
您在 R
中使用的 ifelse
已替换为 。
对此的变化:
您 没有 使用 assign
:如果需要,您可以直接在 df
上创建一个新列而无需创建副本:
df['col'] = ['pos' if a > 0 else 'neg' for a in df['a']]
此外,您可以使用 numpy
的矢量化方法之一来代替列表推导式条件语句,例如 np.select
:
import numpy as np
df['col'] = np.select([df['a'] > 0], ['pos'], 'neg')
# or
df = df.assign(col = np.select([df['a'] > 0], ['pos'], 'neg'))
我是一名试图进入 Python 的 R 程序员。在 R 中,当我想有条件地改变列时,我使用:
col = dplyr::mutate(col, ifelse(condition, if_true(x), if_false(x))
在Python中,如何有条件地改变列值?这是我的最小可重现示例:
def act(cntnt):
def do_thing(cntnt):
return(cntnt + "has it")
def do_other_thing(cntnt):
return(cntnt + "nope")
has_abc = cntnt.str.contains.contains("abc")
if has_abc == T:
cntnt[has_abc].apply(do_thing)
else:
cntnt[has_abc].apply(do_other_thing)
您可以使用条件(及其否定)进行逻辑索引:
has_abc = cntnt.str.contains("abc")
cntnt[ has_abc].apply(do_thing)
cntnt[~has_abc].apply(do_other_thing)
我想你要找的是 assign
,它本质上是 pandas 等同于 dplyr
中的 mutate
。您的条件语句可以使用列表理解或使用矢量化方法编写(见下文)。
以数据框为例,我们称它为df
:
> df
a
1 0.50212013
2 1.01959213
3 -1.32490344
4 -0.82133375
5 0.23010548
6 -0.64410737
7 -0.46565442
8 -0.08943858
9 0.11489957
10 -0.21628132
R
/ dplyr
:
在R
中,您可以使用mutate
和ifelse
来根据条件创建一个列(在这个例子中,当列a时它将是'pos'
大于 0
):
df = dplyr::mutate(df, col = ifelse(df$a > 0, 'pos', 'neg'))
结果df
:
> df
a col
1 0.50212013 pos
2 1.01959213 pos
3 -1.32490344 neg
4 -0.82133375 neg
5 0.23010548 pos
6 -0.64410737 neg
7 -0.46565442 neg
8 -0.08943858 neg
9 0.11489957 pos
10 -0.21628132 neg
Python
/ Pandas
在 pandas
中,使用带有列表理解的 assign
:
df = df.assign(col = ['pos' if a > 0 else 'neg' for a in df['a']])
结果df
:
>>> df
a col
0 0.502120 pos
1 1.019592 pos
2 -1.324903 neg
3 -0.821334 neg
4 0.230105 pos
5 -0.644107 neg
6 -0.465654 neg
7 -0.089439 neg
8 0.114900 pos
9 -0.216281 neg
您在 R
中使用的 ifelse
已替换为
对此的变化:
您 没有 使用 assign
:如果需要,您可以直接在 df
上创建一个新列而无需创建副本:
df['col'] = ['pos' if a > 0 else 'neg' for a in df['a']]
此外,您可以使用 numpy
的矢量化方法之一来代替列表推导式条件语句,例如 np.select
:
import numpy as np
df['col'] = np.select([df['a'] > 0], ['pos'], 'neg')
# or
df = df.assign(col = np.select([df['a'] > 0], ['pos'], 'neg'))