Alex Nikitin
Alex Nikitin

Reputation: 931

Pandas apply function to column and fill 2 columns from function return

Suppose i have a following dataframe

| domain           | category | confidence  

| www.test.com.    |          | 

| www.someurl.com  |          |

I want to apply my_func to domain column. This function return tuple with two values, i want to fill category and confidence with those values for every row. Something like df['category', 'confidence'] = df['domain'].apply(my_func)

The result i expecting is

| domain           | category       | confidence  

| www.test.com.    | test-category  |   0.5

| www.someurl.com  |  some-category |   0.7

Upvotes: 1

Views: 478

Answers (2)

samusa
samusa

Reputation: 483

If you use the current pandas version you can do that with result_type='expand'. from the pandas apply documentation:

>>>df.apply(lambda x: [1, 2], axis=1, result_type='expand')
   0  1
0  1  2
1  1  2
2  1  2

And the solution from @Andrej Kesely is also there stated:

Returning a Series inside the function is similar to passing result_type='expand'. The resulting column names will be the Series index.

df.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1)
   foo  bar
0    1    2
1    1    2
2    1    2

Upvotes: 3

Andrej Kesely
Andrej Kesely

Reputation: 195408

You can return pd.Series. For example:

cnt = 0


def my_func(x):
    global cnt
    cnt += 10
    return pd.Series(["something {}".format(x), cnt])


df[["category", "confidence"]] = df["domain"].apply(my_func)
print(df)

Prints:

            domain                   category  confidence
0    www.test.com.    something www.test.com.          10
1  www.someurl.com  something www.someurl.com          20

Upvotes: 1

Related Questions