Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

impute data by using groupby and transform

df['value'] = df.groupby(['category', 'name'])['value']
    .transform(lambda x: x.fillna(x.mean()))
Comment

PREVIOUS NEXT
Code Example
Python :: python find cells with na 
Python :: can i call a python script from a function 
Python :: bst deleting 
Python :: python 3 docs 
Python :: Return array of odd rows and even columns from array using numpy 
Python :: django insert data into database foreign key view.py 
Python :: create django model field based on another field 
Python :: structural pattern matching python 
Python :: transform dictionary keys python 
Python :: compare string python 
Python :: least recently used cache 
Python :: pytest snapshot update 
Python :: tkinter standard dialogs message 
Python :: reverse string in python without using function 
Python :: sudo in python 
Python :: df shape 
Python :: python how to write array into matlab file 
Python :: bytestring python 
Python :: jupyter dataframe print all 
Python :: python filter list 
Python :: gene wilder pure imagination 
Python :: keras backend matrix multiplication 
Python :: label with list comprehension python 
Python :: threadpool python map 
Python :: python greater than dunder 
Python :: mistborn series 
Python :: recall at k calculate python 
Python :: how to take first half of list python 
Python :: .dropna() python 
Python :: model.predict knn 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =