基于列的 pandas 多条件函数 [英] Pandas Multiple Conditions Function based on Column
问题描述
只是试图找到一种最优雅的方法来对每个不同的列中的值进行真正简单的转换,而每个列都有其自身的条件.因此,给定这样的数据框:
Just trying to find the most elegant way to apply a really simple transformation to values in different columns with each column having it's own condition. So given a dataframe like this:
A B C D E F
0 1 2013-01-02 1 3 test foo
1 1 2013-01-02 1 3 train foo
2 1 2013-01-02 1 3 test foo
3 1 2013-01-02 1 3 train foo
只想拥有一个仅在第二列具有特定值时才调整每列中的值的函数.换句话说...
Just want to have a function that will adjust the values in each column only if a second column has a specific value. In other words...
df['C'] = -1 if df['E'] == "test" else df['C'] next column...
df['D'] = -2 if df['E'] == "test" else df['D'] and so forth.
我当时在想熊猫的where功能会派上用场,但不确定如何应用.我可以执行以下操作,但效率似乎不高,因此我必须为每个col创建一个不同的函数:
I was thinking the where function in pandas would come in handy here but wasn't sure how to apply it. I could do the below but does not seem very efficient and I would have to create a different function for each col:
def col(df):
if df['col1'] == "value":
return -1.00
else:
return relative_buckets['col1']
推荐答案
您可以将.loc
与布尔序列一起使用:
You can use .loc
with a boolean series:
>>> df
A B C D E F
0 1 2013-01-02 1 3 test foo
1 1 2013-01-02 1 3 train foo
2 1 2013-01-02 1 3 test foo
3 1 2013-01-02 1 3 train foo
>>> df.loc[df.E == "test", "C"] = -1
>>> df
A B C D E F
0 1 2013-01-02 -1 3 test foo
1 1 2013-01-02 1 3 train foo
2 1 2013-01-02 -1 3 test foo
3 1 2013-01-02 1 3 train foo
由于视图和复制问题,使用.loc
比尝试直接影响列更可取(请参见
Using .loc
is preferable to trying to affect columns directly because of view vs. copy issues (see here for the gory details.)
如果您想一次更改多个列,也可以执行以下操作:
If you want to change multiple columns at once, you can do that too:
>>> df.loc[df.E == "test", ["C","D"]] = [888, 999]
>>> df
A B C D E F
0 1 2013-01-02 888 999 test foo
1 1 2013-01-02 1 3 train foo
2 1 2013-01-02 888 999 test foo
3 1 2013-01-02 1 3 train foo
这篇关于基于列的 pandas 多条件函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!