Update Multiple Columns using Pandas Apply Functio

2019-08-20 00:50发布

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

Existing code

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

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

Wanted to know if there is any way to do the same thing for any number of columns like

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

标签: lambda
1条回答
一纸荒年 Trace。
2楼-- · 2019-08-20 01:21

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
查看更多
登录 后发表回答