0

I have a DataFrame with millon of rows and a lot of NaN values. Some example:

index     Company        Area
    0     Google         Technology
    1     Coca Cola      Drinks
    2     NaN            Drinks
    3     Apple          Technology
    4     NaN            Technology
    5     Gatorade       Drinks
    6     Dell           Technology
    7     Apple          Technology
    8     Coca Cola      Drinks
    9     NaN            Drinks
    10    Google         Technology

My idea is to fill Companies NaN values with one of the 2 most common values for its Area.

From example: If the most frequent Companies in Technology area are Apple and Google, I Would like to fill the "df['Area'] == 'Technology'" NaN values with one of that values (randomly)

I've already created a Group By DataFrame with the most common values, it is something like this:

Area          Company
Technology    Google
Technology    Apple
Drinks        Coca Cola
Drinks        Pepsi

The result should be something like this:

index     Company        Area
    0     Google         Technology
    1     Coca Cola      Drinks
    2     Pepsi          Drinks
    3     Apple          Technology
    4     Google         Technology
    5     Gatorade       Drinks
    6     Dell           Technology
    7     Apple          Technology
    8     Coca Cola      Drinks
    9     Pepsi          Drinks
    10    Google         Technology

I hope you can help me.

Thanks!!!

2
  • Should all NaN values for a given key be filled by the same value (chosen randomly)? Your question isn't that clear. Commented Jun 12, 2018 at 2:18
  • @coldspeed not, it should be random filled with one of the top 2 values into its Category. For example, some Technologies NaN values should be filled with "Google" and some others with "Apple". Commented Jun 12, 2018 at 2:26

1 Answer 1

0

I come up with this solution by using random.choice

import random

s=df1.groupby('Area').Company.apply(list).reindex(df.Area).apply(lambda x :random.choice(x) )
s.index=df.index

df.Company=df.Company.fillna(s)

df
Out[200]: 
    index   Company        Area
0       0    Google  Technology
1       1  CocaCola      Drinks
2       2  CocaCola      Drinks
3       3     Apple  Technology
4       4    Google  Technology
5       5  Gatorade      Drinks
6       6      Dell  Technology
7       7     Apple  Technology
8       8  CocaCola      Drinks
9       9     Pepsi      Drinks
10     10    Google  Technology
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work. I have an error: TypeError: object of type 'float' has no len()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.