Answers for "impute data by using groupby and transform"

0

impute data by using groupby and transform

import pandas as pd
from datetime import datetime

def generate_data():
    ...

t = datetime.now()
df = generate_data()
df['value'] = df.groupby(['category', 'name'])['value']\
    .transform(lambda x: x.fillna(x.mean()))
print(datetime.now()-t)

# 0:00:00.016012

t = datetime.now()
df = generate_data()
df["value"] = df.groupby(['category', 'name'])\
    .transform(lambda x: x.fillna(x.mean()))['value']
print(datetime.now()-t)

# 0:00:00.030022
Posted by: Guest on August-10-2020
0

impute data by using groupby and transform

df['value'] = df.groupby(['category', 'name'])['value']\
    .transform(lambda x: x.fillna(x.mean()))
Posted by: Guest on August-10-2020

Code answers related to "impute data by using groupby and transform"

Python Answers by Framework

Browse Popular Code Answers by Language