使用列值选择性替换NaN

时间:2019-02-15 08:02:51

标签: python pandas dataframe

我有一个数据框:

   GType   BNM  XXCV  COO
0  CAT1  WERT   NaN  NaN
1  CAT3  FRGT   NaN  NaN
2  CAT3   NaN   NaN  NaN
3  CAT2   NaN   NaN  NaN

我需要使用NaN"vvv"替换为空格或文本GType

我有以下代码:

list1 = ['CAT1','CAT2']
mask = (df.COO.isnull()|df.BNM.isnull()|df.XXCV.isnull()) & (df.GType.isin(list1))
df.loc[mask, ('COO',  'BNM', 'XXCV')] = df.loc[mask, ('COO', 'XXCV', 'BNM'].replace('NaN', '')

list12 = ['CAT3']
mask12 =df.COO.isnull()|df.BNM.isnull())& (df.GType.isin(list12))
df.loc[mask12, ('COO',  'BNM')] = df.loc[mask12, ('COO', 'BNM')].replace('NaN', '')

它运行并更改list1中的GType项,并将其保留在list12中。此刻我得到:

   GType   BNM XXCV  COO
0  CAT1  WERT          
1  CAT3  FRGT  NaN  NaN
2  CAT3   NaN  NaN  NaN
3  CAT2   NaN

我期望的结果是:

   GType   BNM XXCV  COO
0  CAT1  WERT          
1  CAT3  FRGT  NaN  
2  CAT3         NaN 
3  CAT2   

我在哪里弄错了?

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题

import pandas as pd

df = pd.DataFrame({
    'GType': ['CAT1', 'CAT3', 'CAT3', 'CAT2'],
    'BNM': ['WERT', 'FRGT', None, None],
    'XXCV': [None, None, None, None],
    'COO': [None, None, None, None],
})

to_nan_xxcv = (df.XXCV.isnull()) & (df.GType.isin(['CAT1', 'CAT2']))
to_nan_rest = (df.COO.isnull() | df.BNM.isnull())
df.loc[to_nan_xxcv, 'XXCV'] = df.loc[to_nan_xxcv, 'XXCV'].fillna('')
df.loc[to_nan_rest, ('COO', 'BNM')] = df.loc[to_nan_rest, ('COO', 'BNM')].fillna('')
print(df)

to_nan_xxcv仅过滤出您想要的内容,其余的将独立地替换在GType上,这显然是您根据所需输出想要的内容。 另外,您可以通过调用vvv

替换为.fillna('vvv')