Pandas DataFrame:如何更改列的值?

3

我有一个df,其中两个列的值为TrueFalseNaN

df

            a         b           c

    0      a         True        NaN
    1      b         False       True
    2      c         NaN         False
    3      d         NaN         NaN
    4      e         False       NaN
    5      f         True        False

我正在尝试转换列 bc 中的值。

如果一列中出现 True,另一列将变为 False。如果存在 NaN,则将其更改为 False

对于 False,同样地,如果两列都存在 NaN,则将两个值都更改为 False

结果 df:

            a         b           c

    0      a         True        False
    1      b         False       True
    2      c         True        False
    3      d         False       False
    4      e         False       True
    5      f         True        False
5个回答

5

让我们尝试两步填充:

s = df[['b','c']]

# both are `NaN`, fill with `False`
df.loc[s.isna().all(1), ['b','c']] = False

# inverse the sum 
sums = (1 - s.sum(1)).astype(bool)

# fill the remaining `NaN` with the inverse sum
df[['b','c']] = s.apply(lambda x: x.fillna(sums))

输出:

   a      b      c
0  a   True  False
1  b  False   True
2  c   True  False
3  d  False  False
4  e  False   True
5  f   True  False

两个 NaN 都被改成了 True 而不是 False - luknajirku

1

有三种内置的Python方法可以根据条件更改列值。您可以参考下面的链接: https://pythonexamples.org/pandas-dataframe-replace-values-in-column-based-on-condition/

我的问题陈述是,对于数据框(df2)中的列ACTIONCODE,如果列值为A / AMEND / correction,则将其更改为MODIFY。

df2.loc[((df2.ACTIONCODE == 'A') | (df2.ACTIONCODE == 'AMEND') | (df2.ACTIONCODE == 'correction')), 'ACTIONCODE'] = 'MODIFY'

注意:or和and语句需要真值。对于pandas来说,这被认为是模糊的,因此您应该使用“按位” |(或)或&(和)操作。


0

这里使用 numpy where 来检查每一列的条件并替换为相反的值:

cond1 = df.b.notna() & df.c.isna()
cond2 = df.b.isna() & df.c.notna()

df.assign(
    c=lambda x: np.where(cond1, df.b.sub(1).ne(0), df.c),
    b=lambda x: np.where(cond2, df.c.sub(1).ne(0), df.b),
).mask(lambda x: x.isna(), False)


    a   b        c
0   a   True    False
1   b   False   True
2   c   True    False
3   d   False   False
4   e   False   True
5   f   True    False

或者,您可以使用.loc并分配值:

cond3 = df.b.isna() & df.c.isna()

df.loc[cond1, "c"] = df.loc[cond1, "b"].sub(1).ne(0)
df.loc[cond2, "b"] = df.loc[cond2, "c"].sub(1).ne(0)
df.loc[cond3, ["b", "c"]] = False

0
创建一个用户自定义函数 swap:
import numpy as np


def swap(x):
  if(x['b']==False and x['c']==np.NaN):
    return True
  if(x['b']==True and x['c']==np.NaN):  
    return False
  else:
    return c  
 def swap2(x):
      if(x['c']==False and x['b']==np.NaN):
        return True
      if(x['c']==True and x['b']==np.NaN):  
        return False
      else:
        return b

然后使用apply函数:

df['c']=df.apply(swap, axis=1)

df['b']=df.apply(swap2, axis=1)

然后用 false 填充 NAs

df.fillna(False)

0
创建筛选条件。
na_false = (df['b'].isna()) & (df['c'] == False)

na_true = (df['b'].isna()) & (df['c'] == True)

true_na = (df['b'] == True) & (df['c'].isna())

false_na = (df['b'] == False) & (df['c'].isna())

na_na = (df['b'].isna()) & (df['c'].isna())

使用过滤器设置列值

df.loc[na_false,'b'] = True

df.loc[na_true,'b'] = False

df.loc[true_na, 'c'] = False

df.loc[false_na, 'c'] = True

df.loc[na_na, ['b', 'c']] = False

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接