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 :: pandas drop a list of rows 
Python :: how to read a data file in python and build a list of files 
Python :: assert vs validate in python 
Python :: start of the american labor movement 
Python :: raspberry pi pygame play thru bluetooth device 
Python :: using list comprehension to filter out age group pandas 
Python :: sns.distplot fit 
Python :: delete add replace conttent from csv by using python 
Python :: python library automatic sort 
Python :: knn example 
Python :: connect kaggle to colab 
Python :: inicair venv python 
Python :: keepalive_requests 
Python :: how to add existiong database in dango 
Python :: py 
Python :: what does 0 for in array mean python 
Python :: ggt euklidischer algorithmus python 
Python :: tkinter set widht 
Python :: what is enumerate in python 
Python :: Ipython.display latex in the IDE like spyder 
Python :: tyjacsav 
Python :: how to map data to a scale python 
Python :: how to find pandoc template folder 
Python :: can paypal be hacked by email 
Python :: Unpacking list using an asterisk 
Python :: The Model display 
Python :: List Creating List 
Python :: how to apply tanH on pd dataframe 
Python :: instabot source code python library 
Python :: printing multiple input in python 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =