使用Pandas Apply功能更新多列 [英] Update Multiple Columns using Pandas Apply Function

查看:572
本文介绍了使用Pandas Apply功能更新多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pandas dataframe apply函数更新多列.我可以成功完成一栏.

I am trying to update the multiple columns using pandas dataframe apply function. I am successfully able to do this for one column.

现有代码

def funcgetsh(input):
...     hash_object = hashlib.sha1(input)
...     return hash_object.hexdigest()

df["col_1"]=df["col_1"].apply(funcgetsh)

想知道是否可以对

df["col_1","col_2","col_3"]=df["col_1","col_2","col_3"].apply(funcgetsh)

推荐答案

尝试将df["col_1","col_2","col_3"]=df["col_1","col_2","col_3"].apply(funcgetsh)修改为df[["col_1","col_2","col_3"]]=df[["col_1","col_2","col_3"]].apply(funcgetsh).参见下面的示例.

Try modifying df["col_1","col_2","col_3"]=df["col_1","col_2","col_3"].apply(funcgetsh) to df[["col_1","col_2","col_3"]]=df[["col_1","col_2","col_3"]].apply(funcgetsh). See example below.

import pandas as pd

data1 = {"col_1": [1, 2, 3],
        "col_2": [4, 5, 6],
        'col_3': [7, 8, 9]}

df1 =pd.DataFrame(data1)

print(df1)

   col_1  col_2  col_3
0      1      4      7
1      2      5      8
2      3      6      9

def times10(x):
    return 10*x

df1[['col_1', 'col_2']] = df1[['col_1', 'col_2']].apply(times10)

print(df1)

   col_1  col_2  col_3
0     10     40      7
1     20     50      8
2     30     60      9

此替代方法应为您工作,用示例替换您的功能.

This workaround should work for you, replace your function with the example.

import pandas as pd

data1 = {"col_1": [1, 2, 3],
        "col_2": [4, 5, 6],
        'col_3': [7, 8, 9]}

df1 =pd.DataFrame(data1)

# combine columns you want to apply the function to
working_data = df1[['col_1', 'col_2']]

# drop the original values from the columns being altered
# keep unaltered columns
df2 = df1.drop(['col_1', 'col_2'], axis = 1)

# your function here
def times10(x):
    return 10*x

# apply function to the columns/values desired
working_data = working_data.apply(times10)

# merge post function columns/values with the original unaltered columns/values
final_df = pd.merge(working_data, df2, how = 'inner', left_index = True, right_index = True)

print(final_df)

   col_1  col_2  col_3
0     10     40      7
1     20     50      8
2     30     60      9

这篇关于使用Pandas Apply功能更新多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆