使用一个自定义函数更新多个列

2024-09-28 22:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要清理数据框中的许多列;因此,我通过dataframe列上的apply方法定义并使用了多个函数

一个虚构的例子:

def fn_a(x):
    if x<50:
        return 'OK'
    else:
        return 'not OK'

def fn_b(x):
    if x<=40:
        return 'too small'
    elif x>40 and x<70:
        return 'just right'
    else:
        return 'too high'
    
df = pd.DataFrame(np.random.randint(0, 100, size=(5, 2)), columns=['a','b'])
df['a'] = df['a'].apply(fn_a)
df['b'] = df['b'].apply(fn_b)

是否有一种方法可以只应用一个函数,即定义一个fn()函数并将其传递到apply方法,而不是逐列传递?换句话说,我应该在fn中添加什么

def fn(x):
    ...

df = df.apply(fn)

够了吗


Tags: 数据方法函数dataframedfreturnif定义
2条回答

可复制数据:

np.random.seed(0)
df = pd.DataFrame(np.random.randint(0, 100, size=(5, 2)), columns=['a','b'])
    a   b
0  44  47
1  64  67
2  67   9
3  83  21
4  36  87

使用熊猫应用:

如果确实要使用apply进行此操作,可以设置axis=1并使用x[colname]检索列名,其中x是当前行:

def fn_a(x):
    x['a'] = 'OK' if x['a'] < 50 else 'not OK'
    if x['b'] <= 40:
        x['b'] = 'too small'
    elif x['b'] > 40 and x['b'] < 70:
        x['b'] = 'just right'
    else:
        x['b'] = 'too high'
    return x

df = df.apply(fn_a, axis=1)
print(df)

输出:

        a           b
0      OK  just right
1  not OK  just right
2  not OK   too small
3  not OK   too small
4      OK    too high

使用矢量化方法

可以考虑使用^{}^{}。另外,看看^{}。您可以设置一个函数来更新数据帧就地

def fn(df):
    df['a'] = np.where(df.a.lt(50), 'OK', 'not OK')
    df['b'] = np.select(
        condlist=[df.b.le(40), df.b.gt(40) & df.b.lt(70)],
        choicelist=['too small', 'just right'],
        default='too high'
    )
    
fn(df)
print(df)

输出:

        a           b
0      OK  just right
1  not OK  just right
2  not OK   too small
3  not OK   too small
4      OK    too high

如果不想在位置中修改数据帧,请在函数中进行复制,修改复制的数据帧,然后返回:

def fn(df):
    df = df.copy()
    df['a'] = np.where(df.a.lt(50), 'OK', 'not OK')
    df['b'] = np.select(
        condlist=[df.b.le(40), df.b.gt(40) & df.b.lt(70)],
        choicelist=['too small', 'just right'],
        default='too high'
    )
    return df

df = fn(df)
print(df)

返回相同的输出

尝试:

def fn(x1, x2):
    return [fn_a(x1), fn_b(x2)]

df[['c', 'd']] = df.apply(lambda row: fn(row.a, row.b), axis=1).values.tolist()

相关问题 更多 >