【问题标题】:Replace values in Columns替换列中的值
【发布时间】:2020-05-20 20:59:12
【问题描述】:

我想使用 if 循环替换列中的值:

如果 [D] 列中的值与 [A,B,C] 中的任何值都不相同,则将第一个 NaN 的列替换为 D,如果一行中没有 NaN,则创建一个新列 [E]并从列 [E] 中的列 [D] 中添加值。

ID    A    B    C    D 

0    22   32   NaN   22
1    25   13   NaN   15
2    27   NaN  NaN   20
3    29   10   16    29
4    12   92   33    55

我希望输出是:

ID    A     B    C     D    E

 0    22   32   NaN   22
 1    25   13   15    15
 2    27   20   NaN   20
 3    29   10   16    29
 4    12   92   33    55   55

【问题讨论】:

  • 请考虑提供您目前尝试过的解决方案,否则会让您看起来像是在请人为您编写代码。

标签: python-3.x pandas dataframe for-loop conditional-statements


【解决方案1】:
List = [[22  , 32 ,  None ,  22],
        [25  , 13 ,  None ,  15],
        [27  , None ,  None ,  20],
        [29  , 10 ,  16 ,  29],
        [12  , 92 ,  33 ,  55]]

for Row in List:
    Target_C = Row[3]
    if Row.count(Target_C) < 2:                      # If there is no similar condetion pass 
        None_Found = False                           # Small bool to check later if there is no None !
        for enumerate_Column in enumerate(Row):      # get index for each list
            if(None in enumerate_Column):            # if  there is None gin the row
                Row[enumerate_Column[0]] = Target_C  # replace None with column D
                None_Found = True                    # Change None_Found to True
            if(None_Found):                          # Break the loop if found None
                break                                
        if(None_Found == False):                     # if you dont found None add new clulmn
            Row.append(Target_C)

My Code example

【讨论】:

    【解决方案2】:

    你可以这样做

    a = df.isnull()  
    b = (a[a.any(axis=1)].idxmax(axis=1)) 
    nanindex = b.index
    
    check = (df.A!=df.D) & (df.B!=df.D) & (df.C!=df.D)
    commonind = check[~check].index
    
    replace_ind_list = list(nanindex.difference(commonind))
    new_col_list = df.index.difference(list(set(commonind.tolist()+nanindex.tolist()))).tolist()
    
    df['E']=''
    for index, row in df.iterrows():
        for val in new_col_list:
            if index == val:
                df.at[index,'E'] = df['D'][index]
        for val in replace_ind_list:
            if index == val:
                df.at[index,b[val]] = df['D'][index]
    df
    

    输出

        ID  A   B        C       D  E
    0   0   22  32.0    NaN     22  
    1   1   25  13.0    15.0    15  
    2   2   27  20.0    NaN     20  
    3   3   29  10.0    16.0    29  
    4   4   12  92.0    33.0    55  55
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 2019-09-07
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多