简体   繁体   English

根据条件更改单元格值

[英]Change cell value with condition

I have a dataframe:我有一个 dataframe:

df = pd.DataFrame(
        {'a': ['banana', 'coconut', 'banana', 'apple'],
         'b': ['rice', 'bean', 'rice', 'soap'],
         'c': ['mouse', 'dog', None,'apple'],
         'd': ['cat', 'soap', 'beef', 'rabbit']}
    )


         a     b     c       d
0   banana  rice   mouse  cat
1  coconut  bean   dog    soap
2   banana  rice  None    cat
3    apple  soap  apple   rabbit

If a line contains the value None (here in index 2), we look for the line whose values are exactly the same and change the value of None by that of the same column.如果一行包含值 None(此处在索引 2 中),我们将查找其值完全相同的行,并将 None 的值更改为同一列的值。 So here the row of index 0 and the row of index 2 have the same values except in column 'c'.因此,此处索引 0 的行和索引 2 的行具有相同的值,但“c”列除外。 We then change None by 'cat' The expected result is therefore:然后我们将 None 更改为 'cat' 因此预期结果是:

         a     b     c       d
0   banana  rice   mouse   cat
1  coconut  bean   dog     soap
2   banana  rice   mouse   cat
3    apple  soap   apple   rabbit

Quelqu'un à une solution à cette probleme, merci Quelqu'un à une solution à cette probleme, 谢谢

df.loc[df['c'].isnull(), 'c'] = df[df.duplicated(subset = ['a', 'b'], keep = 'last')]['c'].values

df

Output: Output:

|index|    a    | b  |  c  |  d   |
|-----|---------|----|-----|------|
|  0  | banana  |rice|mouse| cat  |
|  1  | coconut |bean| dog | soap |
|  2  | banana  |rice|mouse| beef |
|  3  | apple   |soap|apple|rabbit|

This code would do the trick for any number of None s:这段代码可以解决任意数量的None问题:

In [183]: df = pd.DataFrame(
     ...:         {'a': ['banana', 'coconut', 'banana', 'apple', None],
     ...:          'b': ['rice', 'bean', 'rice', 'soap', 'soap'],
     ...:          'c': ['mouse', 'dog', None, 'apple', 'apple'],
     ...:          'd': ['cat', 'soap', 'cat', 'rabbit', None]}
     ...:     )

In [184]: df
Out[184]: 
         a     b      c       d
0   banana  rice  mouse     cat
1  coconut  bean    dog    soap
2   banana  rice   None     cat
3    apple  soap  apple  rabbit
4     None  soap  apple    None

In [185]: rows = df.isnull().any(axis=1).to_numpy().nonzero()[0] # rows with None
     ...: for i in rows:
     ...:     row = df.iloc[i]
     ...:     cols = df.columns[row.notnull()] # columns without None
     ...:     replacement = (df[cols] == row[cols]).all(axis=1).to_numpy().nonzero()[0]
     ...:     for j in replacement:
     ...:         if i != j:
     ...:             df.loc[i] = df.loc[j]
     ...:             break

In [186]: df
Out[186]: 
         a     b      c       d
0   banana  rice  mouse     cat
1  coconut  bean    dog    soap
2   banana  rice  mouse     cat
3    apple  soap  apple  rabbit
4    apple  soap  apple  rabbit

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

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