1

There two columns in the DataFrame named "country" & "taster_name". Column "taster_name" got some missing values in it. I want to fillna the missing values with the MAX VALUE_COUNTS of the taster_name of each country(depending on which country the missing value belongs to). I don't know how I can make it.

From the code below, we can check the MAX VALUE_COUNTS of the taster_name of each country.

wine[['country','taster_name']].groupby('country').taster_name.value_counts()
Evan.Chen
  • 69
  • 1
  • 6
  • 1
    please read the [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Brown Bear May 31 '18 at 12:52
  • 2
    [Please don't post images of code (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael May 31 '18 at 12:53
  • OK,thanks for reminding. – Evan.Chen May 31 '18 at 13:16

1 Answers1

0

try this,

df.groupby('country')['teaser_name'].apply(lambda x:x.fillna(x.value_counts().index.tolist()[0]))

As you didn't provide sample data. I created by myself.

Sample Input:

   country teaser_name
0        A     abraham
1        B       silva
2        A     abraham
3        A         NaN
4        B         NaN
5        C        john
6        C         NaN
7        C        john
8        C       jacob
9        A         NaN
10       B       silva
11       A     william

Output:

   country teaser_name
0        A     abraham
1        B       silva
2        A     abraham
3        A     abraham
4        B       silva
5        C        john
6        C        john
7        C        john
8        C       jacob
9        A     abraham
10       B       silva
11       A     william

Explanation: Try to groupby country and fill NaN values with value_counts. In value_counts by default it's in descending order. So, you can take first element and fill with NaN.

Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
  • using transform instead of apply ```df['teaser_name']=df.groupby('country'['teaser_name'].transform(lambdax:x.fillna(x.value_counts().index.tolist()[0]))``` – Sidhom Apr 17 '19 at 10:30