Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在iloc NULL上设置值_Python_Pandas_Dataframe - Fatal编程技术网

Python 在iloc NULL上设置值

Python 在iloc NULL上设置值,python,pandas,dataframe,Python,Pandas,Dataframe,下面我有一个示例数据帧和函数。我创建了一个函数,该函数将获取“cell”的坐标,并将其放入一个元组,以及将其放入元组的原因。我想让这个函数也改变某一列的值 import pandas as pd import numpy as np df1 = pd.DataFrame({'A' : [np.NaN,np.NaN,3,4,5,5,3,1,5,np.NaN], 'B' : [1,0,3,5,0,0,np.NaN,9,0,0],

下面我有一个示例数据帧和函数。我创建了一个函数,该函数将获取“cell”的坐标,并将其放入一个元组,以及将其放入元组的原因。我想让这个函数也改变某一列的值

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A' : [np.NaN,np.NaN,3,4,5,5,3,1,5,np.NaN], 
                    'B' : [1,0,3,5,0,0,np.NaN,9,0,0], 
                    'C' : [10,0,30,50,0,0,4,10,1,0], 
                    'D' : [1,0,3,4,0,0,7,8,0,1],
                    'E' : [np.nan,'Unassign','Assign','Ugly','Appreciate',
                          'Undo','Assign','Unicycle','Assign','Unicorn',]})
print(df1)
highlights = []
def find_nan(list_col):
    for c in list_col:
        # if column is one of the dataframe's columns, go
        if c in df1.columns:
            # for each index x where column c of the dataframe is null, go
            for x in df1.loc[df1[c].isnull()].index: #appends to the list of tuples
                highlights.append(
                    tuple([x + 2, df1.columns.get_loc(c) + 1, f'{c} is Null in row {x + 2}']))

                df1.iloc[x, df1.columns.get_loc('E')] = f'{c} is blank in row {x + 2}'
find_nan(['A','B']) 
    # using the function above, checks for all nulls in A and B
    # Also places the coordinates and reason in a tuple and changes values of column 'E'

    #output:
    A   B   C   D   E
0   NaN 1.0 10  1   A is blank in row 2
1   NaN 0.0 0   0   A is blank in row 3
2   3.0 3.0 30  3   Assign
3   4.0 5.0 50  4   Ugly
4   5.0 0.0 0   0   Appreciate
5   5.0 0.0 0   0   Undo
6   3.0 NaN 4   7   Assign
7   1.0 9.0 10  8   Unicycle
8   5.0 0.0 1   0   Assign
9   NaN 0.0 0   1   A is blank in row 11
我想做的是添加逻辑,如果
E
已填充,则将原因添加在一起,或者如果为null,则只需更改
E
的值。我的问题是:使用
df1.iloc
我似乎无法检查空值

df1.iloc[0]['E'].isnull()
返回
AttributeError:'float'对象没有属性'isnull'
(显然)

为了解决这个问题:我可以使用if
np.isnan(df1.iloc[0]['E'])
,它的计算结果为
True
,但是如果
E
中已经有一个值,我将得到一个
TypeError

本质上,我想要的是我的函数中的这种逻辑:

if df1.iloc[x]['E'] is null:
    df1.iloc[x, df1.columns.get_loc('E')] = 'PREVIOUS_VALUE' + f'{c} is blank in row {x + 2}'
else:
    df1.iloc[x, df1.columns.get_loc('E')] = f'{c} is blank in row {x + 2}
原始数据帧上my函数的预期输出:

find_nan(['A','B'])

    A   B   C   D   E
0   NaN 1.0 10  1   A is blank in row 2
1   NaN 0.0 0   0   Unassign and A is blank in row 3
2   3.0 3.0 30  3   Assign
3   4.0 5.0 50  4   Ugly
4   5.0 0.0 0   0   Appreciate
5   5.0 0.0 0   0   Undo
6   3.0 NaN 4   7   Assign and B is blank in row 8
7   1.0 9.0 10  8   Unicycle
8   5.0 0.0 1   0   Assign
9   NaN 0.0 0   1   Unicorn and A is blank in row 11

使用Python3.6。这是一个具有更多功能的更大项目的一部分,因此“添加原因”和向索引中添加2“没有明显原因”

注意,这是使用Python2测试的,但我没有注意到任何可能的情况 防止它在Python3中工作

def find_nan(df, cols):
    if isinstance(cols, (str, unicode)):
        cols = [cols]  # Turn a single column into an list.
    nulls = df[cols].isnull()  # Find all null values in requested columns.
    df['E'] = df['E'].replace(np.nan, "")  # Turn NaN values into an empty string.
    for col in cols:
        if col not in df:
            continue
        # If null value in the column an existing value in column `E`, add " and ".
        df.loc[(nulls[col] & df['E'].str.len().astype(bool)), 'E'] += ' and '
        # For null column values, add to column `E`: "[Column name] is blank in row ".
        df.loc[nulls[col], 'E'] += '{} is blank in row '.format(col)
        # For null column values, add to column `E` the index location + 2.
        df.loc[nulls[col], 'E'] += (df['E'][nulls[col]].index + 2).astype(str)
    return df

>>> find_nan(df1, ['A', 'B'])
    A   B   C  D                                 E
0 NaN   1  10  1               A is blank in row 2
1 NaN   0   0  0  Unassign and A is blank in row 3
2   3   3  30  3                            Assign
3   4   5  50  4                              Ugly
4   5   0   0  0                        Appreciate
5   5   0   0  0                              Undo
6   3 NaN   4  7    Assign and B is blank in row 8
7   1   9  10  8                          Unicycle
8   5   0   1  0                            Assign
9 NaN   0   0  1  Unicorn and A is blank in row 11

避免循环的可能解决方案

def val(ser):
    row_value = ser['index']
    check = ser[['A','B']].isnull()
    found = check[check == True]
    if len(found) == 1:
        found = found.index[0]
        if pd.isnull(ser['E']) == True:
            return found + ' is blank in row ' + str(row_value + 2)
        else:
            return str(ser['E']) + ' and ' + found +' is blank in row ' + str(row_value+2)
    else:
        return ser['E']



df1['index'] = df1.index


df1['E'] = df1.apply(lambda row: val(row),axis=1)
print(df1.iloc[:,:5])

     A    B   C  D                                 E
0  NaN  1.0  10  1               A is blank in row 2
1  NaN  0.0   0  0  Unassign and A is blank in row 3
2  3.0  3.0  30  3                            Assign
3  4.0  5.0  50  4                              Ugly
4  5.0  0.0   0  0                        Appreciate
5  5.0  0.0   0  0                              Undo
6  3.0  NaN   4  7    Assign and B is blank in row 8
7  1.0  9.0  10  8                          Unicycle
8  5.0  0.0   1  0                            Assign
9  NaN  0.0   0  1  Unicorn and A is blank in row 11
如果多列为nan,则编辑

def val(ser):
    check = ser[['A','B']].isnull()
    found = check[check].index
    if len(found) == 0:
        return str(ser['E'])
    else:
        one = (str(ser['E'])+' and ').replace('nan and','')
        two = ' and '.join([str(x) for x in found])
        three = ' is blank in row ' + str(ser['index']+2)
        return (one+two+three).strip()



df1['index'] = df1.index


df1['E'] = df1.apply(lambda row: val(row),axis=1)
print(df1.iloc[:,:5])


     A    B     C  D                                 E
0  NaN  NaN   NaN  1         A and B is blank in row 2
1  NaN  0.0   0.0  0  Unassign and A is blank in row 3
2  3.0  3.0  30.0  3                            Assign
3  4.0  5.0  50.0  4                              Ugly
4  5.0  0.0   0.0  0                        Appreciate
5  5.0  0.0   0.0  0                              Undo
6  3.0  NaN   4.0  7    Assign and B is blank in row 8
7  1.0  9.0  10.0  8                          Unicycle
8  5.0  0.0   1.0  0                            Assign
9  NaN  0.0   0.0  1  Unicorn and A is blank in row 11

避免了Tubleed。如果一行有多个
NaN
值,会发生什么情况?另外,不需要
==True
,例如
检查[check]
工作得很好。@Alexander很公平,只是为了好玩,用一个空字符串替换空值就是诀窍。虽然,这不是一个“干净”的解决方案,因为我希望它是,我会接受它!