ary
ary

Reputation: 151

how to assign new column of values by a specific column?

I have a data like this:

Id     Date   
12     2017
12     2016
13     2014
13     2013
14     2017
14     2015
14     2016

Also I have a list of Ids that if Id is in 'check_list' should assign a value 'On',otherwise assign 'Off' :

check_list= [12,13]

So data should be like this:

Id      Date    Check
12      2017    On
12      2016    On
13      2014    On
13      2013    On
14      2017    Off
14      2015    Off
14      2016    Off

Code

I have tried to define a function but i don't know why it just returns ids, here is the code:

def check():                           
     if df['Id'] in check_list:
         return 'On'
     else:
         return 'Off'


f['Check'] = df['Id'].apply(check()) 

Upvotes: 3

Views: 55

Answers (2)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95873

Your function that you are trying to .apply is wrong, and furthermore, you aren't passing the function, you are calling the function and passing the result:

So, you could do:

In [20]: def check(x):
    ...:      if x in check_list:
    ...:          return 'On'
    ...:      else:
    ...:          return 'Off'
    ...:

In [21]: df['Id'].apply(check)
Out[21]:
0     On
1     On
2     On
3     On
4    Off
5    Off
6    Off
Name: Id, dtype: object

An alternative without apply is to use pd.Series.isin and numpy.where, so, given:

In [23]: df
Out[23]:
   Id  Date
0  12  2017
1  12  2016
2  13  2014
3  13  2013
4  14  2017
5  14  2015
6  14  2016

In [24]: check_list = [12, 13]

You can use isin:

In [25]: df['Id'].isin(check_list)
Out[25]:
0     True
1     True
2     True
3     True
4    False
5    False
6    False
Name: Id, dtype: bool

So, something like:

In [26]: df['Check'] = np.where(df['Id'].isin(check_list), 'On', 'Off')

In [27]: df
Out[27]:
   Id  Date Check
0  12  2017    On
1  12  2016    On
2  13  2014    On
3  13  2013    On
4  14  2017   Off
5  14  2015   Off
6  14  2016   Off

You could also use pd.Series.map:

mapping = dict.fromkeys(check_list, 'On')
df['Check'] = df['Id'].map(mapping).fillna('Off')

df

   Id  Date Check
0  12  2017    On
1  12  2016    On
2  13  2014    On
3  13  2013    On
4  14  2017   Off
5  14  2015   Off
6  14  2016   Off

Upvotes: 4

Tim_M
Tim_M

Reputation: 11

you could also use a list comprehension and pass it to the df like this:

df['Check'] = ['On' if id in [12,13] else 'Off' for id in df.id]

Upvotes: 1

Related Questions