Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

groupby fillna

In [2400]: df
Out[2400]:
   A  B  C    D
0  1  1  1  1.0
1  1  1  1  NaN
2  1  1  1  3.0
3  3  3  3  5.0

In [2401]: df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
Out[2401]:
0    1.0
1    2.0
2    3.0
3    5.0
Name: D, dtype: float64

In [2402]: df['D'] = df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))

In [2403]: df
Out[2403]:
   A  B  C    D
0  1  1  1  1.0
1  1  1  1  2.0
2  1  1  1  3.0
3  3  3  3  5.0
Comment

groupby fillna

In [2396]: df.shape
Out[2396]: (10000, 4)

In [2398]: %timeit df['D'].fillna(df.groupby(['A','B','C'])['D'].transform('mean'))
100 loops, best of 3: 3.44 ms per loop


In [2397]: %timeit df.groupby(['A','B','C'])['D'].apply(lambda x: x.fillna(x.mean()))
100 loops, best of 3: 5.34 ms per loop
Comment

groupby fillna ffill

print (df)
   one  two  three
0    1    1   10.0
1    1    1   40.0
2    1    1    NaN
3    1    2    NaN
4    1    2   20.0
5    1    2    NaN
6    1    3    NaN
7    1    3    NaN

df['three'] = df.groupby(['one','two'], sort=False)['three']
                .apply(lambda x: x.fillna(x.mean()))
print (df)
   one  two  three
0    1    1   10.0
1    1    1   40.0
2    1    1   25.0
3    1    2   20.0
4    1    2   20.0
5    1    2   20.0
6    1    3    NaN
7    1    3    NaN
Comment

groupby fillna ffill

df['three'] = df.groupby(['one','two'], sort=False)['three']
                .apply(lambda x: x.ffill().bfill())
print (df)
   one  two  three
0    1    1   10.0
1    1    1   10.0
2    1    1   10.0
3    1    2   20.0
4    1    2   20.0
5    1    2   20.0
6    1    3    NaN
7    1    3    NaN
Comment

PREVIOUS NEXT
Code Example
Python :: python line number 
Python :: 2d list in python 
Python :: sequence with numbers in python 
Python :: create 2d array with rows and columns 
Python :: How Generate random number in python 
Python :: how to get data after last slash in python 
Python :: stop word python 
Python :: if key not in dictionary python 
Python :: tkinter change button color smoothly 
Python :: ipython and virtualenvs 
Python :: Math Module exp() Function in python 
Python :: get python to run cli commands 
Python :: django rest framework serializers 
Python :: slicing strings in python 
Python :: what is serialization in django 
Python :: python gui kivvy 
Python :: Python How to convert a string to the name of a function? 
Python :: how to add trailing zeros in python 
Python :: disable sns plot python 
Python :: python telegram 
Python :: re module documentation 
Python :: python linux command 
Python :: read mouse log python 
Python :: put cropped image in original image name folder python 
Python :: Add New Column to Pandas from Dictionary 
Python :: install requests-html in linux 
Python :: python code to demonstrate inheritance with animal class 
Python :: facebook python 
Python :: set page title name and favicon in streamlit 
Python :: scikit learn decistion tree 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =