-4

This is my dataset :

 offerid category countty   browserid   devid   click
0   887235  17714   e   Firefox        NaN      0
1   178235  21407   b   Mozilla        Desktop  0
2   518539  25085   a   Edge           NaN      0
3   390352  40339   c   Firefox       Mobile    0
4   472937  12052   d   Mozilla       Desktop   0

For everytime Firefox occurs in 'browserid' column, I need to fill the 'NaN' in the 'devid' column with 'Mobile'. This is the code I've written for achieving this.

if data['browserid'] == 'Firefox' :
    data['devid'].fillna('Mobile', inplace=True)

I am facing the following error :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any help would be aprerciated!

  • https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – Max M Aug 09 '17 at 11:01
  • It's often a good thing to throw the error message into a search engine. –  Aug 09 '17 at 11:02
  • 1
    Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – TehSphinX Aug 09 '17 at 11:06
  • Hey I already checked related questions on this error and none of those solutions worked for me. Hence, this question. I'm new to coding so please bear with me. Thanks for downvotes and the sarcasm. – Dinesh Kesaboina Aug 09 '17 at 12:00

3 Answers3

1
from io import StringIO
import pandas as pd

TESTDATA=StringIO(""" offerid category countty browserid devid click
0 887235 17714 e Firefox NaN 0
1 178235 21407 b Mozilla Desktop 0
2 518539 25085 a Edge NaN 0
3 390352 40339 c Firefox Mobile 0
4 472937 12052 d Mozilla Desktop 0""")

df = pd.read_csv(TESTDATA, sep=" ")
df = df.iloc[:,1:]

df.loc[df['browserid'] == 'Firefox','devid'] = df.loc[df['browserid'] == 'Firefox','devid'].fillna('Mobile')
Clock Slave
  • 7,627
  • 15
  • 68
  • 109
0

Also, you can do something like this:

data.loc[data.browserid == "Firefox",'devid'] = 0 (or "NaN")

But the answer for your question is you compare a column with a string...

Vadim
  • 4,219
  • 1
  • 29
  • 44
0
   offerid  category countty browserid    devid  click
0   887235     17714       e   Firefox   Mobile      0
1   178235     21407       b   Mozilla  Desktop      0
2   518539     25085       a      Edge      NaN      0
3   390352     40339       c   Firefox  Mobile       0
4   472937     12052       d   Mozilla      NaN      0

Read data using pandas
import pandas as pd
df = pd.read_csv('stack_overflow.csv')
df.loc[(df['browserid']=='Firefox')&(df.devid.isnull()),'devid'] ='Mobile'

output 
   offerid  category countty browserid    devid  click
0   887235     17714       e   Firefox   Mobile      0
1   178235     21407       b   Mozilla  Desktop      0
2   518539     25085       a      Edge      NaN      0
3   390352     40339       c   Firefox   Mobile      0
4   472937     12052       d   Mozilla      NaN      0