繁体   English   中英

Pandas DataFrame:更改列值?

[英]Pandas DataFrame: Change column values?

我有一个 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

我正在尝试转换b列和c中的值。

如果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

让我们尝试两步填充:

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))

Output:

   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

有三种内置的 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 和 python 语句需要真值。 对于 pandas 这些被认为是模棱两可的,所以你应该使用“按位”| (or) 或 & (and) 运算

这使用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

创建用户定义的 function 交换:

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

然后使用申请 function:

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

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

然后用 false 填充 NA

df.fillna(False)

为条件创建过滤器

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM