使用熊猫应用功能更新多列

时间:2019-04-17 14:34:54

标签: lambda

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

现有代码

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)

1 个答案:

答案 0 :(得分:0)

尝试将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)。参见下面的示例。

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

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

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