pandas apply를 사용하여 다중 컬럼(multiple columns) 만들기

2020. 6. 9. 23:43분석 Python/Pandas Tip

 

from sklearn.datasets import load_boston
feature = load_boston()
boston_data = pd.DataFrame(feature.data , columns=feature.feature_names)
boston_data.head()
def f(x, col1 , col2) :
    x , y = x[col1] , x[col2]
    plus = x+y
    minus = x-y
    multiply = x*y
    return pd.Series([plus,minus,multiply])
from functools import partial
ff = partial(f , col1 = "CRIM", col2="RM")
boston_data[["plus","minus","multiply"]]= boston_data.apply(lambda x : ff(x),axis=1)
boston_data.head()

 

stackoverflow.com/questions/30026815/add-multiple-columns-to-pandas-dataframe-from-function

 

Add Multiple Columns to Pandas Dataframe from Function

I have a pandas data frame mydf that has two columns,and both columns are datetime datatypes: mydate and mytime. I want to add three more columns: hour, weekday, and weeknum. def getH(t): #gives ...

stackoverflow.com

추가로 RAY로 연습해보기

속도에서는 의미가 없겠지만, 일단 연습...

import ray
ray.init()

@ray.remote
def ray_f(x, col1 , col2) :
    x , y = x[col1] , x[col2]
    plus = x+y
    minus = x-y
    multiply = x*y
    return pd.Series([plus,minus,multiply])
    
results = ray_f.remote(boston_data , col1 = "CRIM", col2="RM")
s = ray.get(results)
pd.DataFrame(s.tolist()).T

728x90