Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get two return values from Pandas apply

Tags:

python

pandas

I'm trying to return two different values from an apply method but I cant figure out how to get the results I need.

With a function as:

def fun(row):
    s = [sum(row[i:i+2]) for i in range (len(row) -1)]
    ps = s.index(max(s))
    return max(s),ps

and df as:

    6:00    6:15    6:30    
0   3       8       9       
1   60      62      116     

I'm trying to return the max value of the row, but i also need to get the index of the first value that produces the max combination.

df["phour"] = t.apply(fun, axis=1)

I can get the output I need, but I don't know how I can get the index in a new column.So far im getting both answer in a tuple

    6:00    6:15    6:30    phour
0   3       8       9       (17, 1)
1   60      62      116     (178, 1)

How can I get the index value in its own column?

like image 625
Daniel Avatar asked Apr 13 '17 17:04

Daniel


People also ask

How can I return multiple values from pandas?

Return Multiple Columns from pandas apply() You can return a Series from the apply() function that contains the new data. pass axis=1 to the apply() function which applies the function multiply to each row of the DataFrame, Returns a series of multiple columns from pandas apply() function.

How do I return two columns in pandas DataFrame?

Return multiple columns using Pandas apply() method. Objects passed to the pandas. apply() are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function.

What is the difference between apply and Applymap in pandas?

apply() is used to apply a function along an axis of the DataFrame or on values of Series. applymap() is used to apply a function to a DataFrame elementwise.

How to return multiple columns using PANDAS' apply (~) function?

To return multiple columns using the apply (~) function in Pandas, make the parameter function return a Series. To create columns B and C, return a Series for the parameter function: Here, foo is called twice, once for each value in df. You can also concatenate the newly created columns with df using Pandas' concat (~) method:

How to get the cell value from the pandas Dataframe?

In this article, we will discuss how to get the cell value from the pandas dataframe. This method is used to get the particular cell data with index function by using the column name dataframe [‘column_name’].loc [dataframe.index [row_number]] Example: Python program to get particular cell using loc () function

How do you return two values from a function in Python?

Let’s take a look at how a function in Python is designed and how to return one value and how to return two values. In the example above, we have defined two different functions, return_one () and return_two (). The former of these returns only a single value. Meanwhile, the latter function, return_two (), returns two values.

What version of PANDAS is used in the apply function?

Prepare the dataset. The version of pandas uses 1.1.5. Solution 01. Returns pd.Series in the apply function. Solution 02. Use result_type ='expand' when applying.


2 Answers

You can apply pd.Series

df.drop('Double', 1).join(df.Double.apply(pd.Series, index=['D1', 'D2']))

   A  B  C  D1  D2
0  1  2  3   1   2
1  2  3  2   3   4
2  3  4  4   5   6
3  4  1  1   7   8

Equivalently

df.drop('Double', 1).join(
    pd.DataFrame(np.array(df.Double.values.tolist()), columns=['D1', 'D2'])
)

setup
using @GordonBean's df

df = pd.DataFrame({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[3,2,4,1], 'Double': [(1,2), (3,4), (5,6), (7,8)]})
like image 105
piRSquared Avatar answered Sep 21 '22 19:09

piRSquared


You can get the index in a separate column like this:

df[['phour','index']] = df.apply(lambda row: pd.Series(list(fun(row))), axis=1)

Or if you modify fun slightly:

def fun(row):
    s = [sum(row[i:i+2]) for i in range (len(row) -1)]
    ps = s.index(max(s))
    return [max(s),ps]

Then the code becomes a little less convoluted:

 df[['phour','index']] = df.apply(lambda row: pd.Series(fun(row)), axis=1)
like image 29
Dmytro Bugayev Avatar answered Sep 23 '22 19:09

Dmytro Bugayev