Python的匿名函数的条件循环,代码为什么总是报错?

我想在一张表中循环判断上面一行和下面一行的前两列的值是否相等,如果相等,fillna的方式选择向上填充,如果不相等,fillna的方式为0。

df.apply(lambda x: x.fillna(method='ffill') if x['一级分类'][i+1]==x['一级分类'][i] and x['报表团队'][i+1]==x['报表团队'][i] else x.fillna(0) for i in range(2256),axis=1)

为什么这么写总是报错呀?

| header | header |

img
一级分类 报表团队 款项日期 价值1 价值2 价值3
非货款 张三 2021-01-01
非货款 张三 2021-01-02 42 468 472920
非货款 张三 2021-01-03
非货款 张三 2021-01-04
货款 李四 2021-01-05
货款 李四 2021-01-06 889 3618 41018
货款 李四 2021-01-07
非货款 王一 2021-01-08
非货款 王一 2021-01-09 5690 4730 1628
非货款 王一 2021-01-10

| ------ | ------ |
| cell | cell |
| cell | cell |

你可以将匿名函数改写成自定义的函数,def proc(x)将数据筛选语句放入到函数中,然后再用apply函数。

写成自定义函数看看

怎么写呀,求教

你把这里的x打印出来看看,x应该是一行或者一列

df["new_col"] = df.apply(lambda row: function1(row), axis=1)

还有你这是很多句吧?那要在外边定义一个方法,再apply这个方法

数据可以提供下 进行测试

import pandas as pd

data = {
    "a":[1,2,3],
    "b":[4,5,6],
    "c":[7,8,9]
}
df = pd.DataFrame(data)

def print_row(row):
    print(row)

df.apply(lambda x: print_row(x), axis=1)
import pandas as pd

current_a = None
current_b = None

data = {
    "a":[1,1,3],
    "b":[2,2,6],
    "c":[7,8,9]
}
df = pd.DataFrame(data)

def print_row(row):
    global current_a
    global current_b

    if current_a == row[0] and current_b == row[1]:
        row.fillna(method='ffill')
        # print(row)
    else:
        row.fillna(0)
        # print(row)

    current_a = row[0]
    current_b = row[1]

df.apply(lambda x: print_row(x), axis=1)
pd.concat((df.iloc[:,[0,1]], df.groupby(list(df.columns[:2])).ffill().bfill().fillna(0)),axis=1)

fillna里使用参数 inplace=True

import pandas as pd

data = {
    "1":["非货款", "张三", "2021-01-01", None, None, None],
    "2":["非货款", "张三", "2021-01-02", "42", "468", "472920"],
    "3":["非货款", "张三", "2021-01-03", None, None, None],
    "4":["非货款", "张三", "2021-01-04", None, None, None],
    "5":["货款", "李四", "2021-01-05", None, None, None],
    "6":["货款", "李四", "2021-01-06", "889", "3618", "41018"],
    "7":["货款", "李四", "2021-01-07", None, None, None],
    "8":["非货款", "王一", "2021-01-08", None, None, None],
    "9":["非货款", "王一", "2021-01-09", "5690", "4730", "1628"],
    "10":["非货款", "王一", "2021-01-10", None, None, None]
}

df = pd.DataFrame(data)

for i in data:
    if int(i) < len(data):
        if df[i][0]==df[str(int(i)+1)][0] and df[i][1]==df[str(int(i)+1)][1]:
            if df[str(int(i)+1)][3] != None:
                df[i].fillna(df[str(int(i)+1)], inplace=True)
            else:
                df[i].fillna(df[str(int(i)-1)], inplace=True)
        else:
            df[i].fillna(0, inplace=True)
    else:
        df[i].fillna(df[str(int(i)-1)], inplace=True)
        
print(df)