How to Return Two Columns with Function

Return multiple columns from pandas apply()

You can return a Series from the applied function that contains the new data, preventing the need to iterate three times. Passing axis=1 to the apply function applies the function sizes to each row of the dataframe, returning a series to add to a new dataframe. This series, s, contains the new values, as well as the original data.

def sizes(s):
s['size_kb'] = locale.format("%.1f", s['size'] / 1024.0, grouping=True) + ' KB'
s['size_mb'] = locale.format("%.1f", s['size'] / 1024.0 ** 2, grouping=True) + ' MB'
s['size_gb'] = locale.format("%.1f", s['size'] / 1024.0 ** 3, grouping=True) + ' GB'
return s

df_test = df_test.append(rows_list)
df_test = df_test.apply(sizes, axis=1)

How to make a function return two columns of different types (R)?

cbind doesn't return a data.frame but a matrix. Try this instead:

TypeCombo <- function(type1, type2, type3) {
data.frame(names = row.names(TypeChart),
values = TypeChart[,type1] * TypeChart[,type2] * TypeChart[,type3])
}

Pandas Apply Function That returns two new columns

Based on your latest error, you can avoid the error by returning the new columns as a Series

def myfunc1(row):
C = row['A'] + 10
D = row['A'] + 50
return pd.Series([C, D])

df[['C', 'D']] = df.apply(myfunc1 ,axis=1)

Apply pandas function to column to create multiple new columns?

Building off of user1827356 's answer, you can do the assignment in one pass using df.merge:

df.merge(df.textcol.apply(lambda s: pd.Series({'feature1':s+1, 'feature2':s-1})), 
left_index=True, right_index=True)

textcol feature1 feature2
0 0.772692 1.772692 -0.227308
1 0.857210 1.857210 -0.142790
2 0.065639 1.065639 -0.934361
3 0.819160 1.819160 -0.180840
4 0.088212 1.088212 -0.911788

EDIT:
Please be aware of the huge memory consumption and low speed: https://ys-l.github.io/posts/2015/08/28/how-not-to-use-pandas-apply/ !

How to return two columns with function

Table functions (functions defined as returns table or returns setof) need to be used in the from clause like a table.

So you need to use:

select * 
from get_avg_prices(...);

Only scalar functions (functions which return only a single value, e.g. a number) should be put into the select list.

Return multiple values from function using apply() and store it in different columns

You can use the following code to modify dataframe inplace. You should change on the dataframe object inside the function directly, else your changes will be lost.

import pandas as pd

df = pd.DataFrame(data={'dates': pd.bdate_range('2020-07-01', '2020-07-31', freq='B')})

def func(row):
df.loc[row.name, 'Day'] = row['dates'].day
df.loc[row.name, 'Month'] = row['dates'].month
df.loc[row.name, 'Year'] = row['dates'].year
print('Done')

df.apply(func, axis=1)

How can a function return multiple columns?

Forget the whole function, you can just use a subquery. I guess you want something like this:

select *
from a_table a
join (select s.com_code, s.p_code, count(*) as Num_SIMRP
from p_stest s
where s.s_status = 'ACTIVE'
and s.s_type like 'H%'
group by s.com_code,s.p_code
having count(s.p_code) > 0) b on b.com_code = a.com_code
and (p_from is null or s.p_code between p_from and p_to)

Dataframe Apply method to return multiple elements (series)

UPDATE

Updated for version 0.23 - using result_type='broadcast' for further details refer to documentation

Redefine your function like this:

def divideAndMultiply(x,y):
return [x/y, x*y]

Then do this:

df[['e','f']] = df.apply(lambda x: divideAndMultiply(x["a"], 2), axis=1, result_type='broadcast')

You shall get the desired result:

In [118]: df
Out[118]:
a b e f
0 0 1 0 0
1 1 2 0 2
2 2 3 1 4
3 3 4 1 6


Related Topics



Leave a reply



Submit