0

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)

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Angel Roman, Thanks for the response. I tried and got the below error TypeError: ('must be convertible to a buffer, not Series', u'occurred at index Col_1')
And When I applied your function to my dataframe it worked fine. like df[["col_1","col_2","col_3"]]=df[["col_1","col_2","col_3"]].apply(times10) but when trying with df[["col_1","col_2","col_3"]]=df[["col_1","col_2","col_3"]].apply(funcgetsh) throwing the error TypeError: ('must be convertible to a buffer, not Series', u'occurred at index Col_1')
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.