0

I am trying to add new field to dataset. If a specific field is in the given list, new value should be 1, if a specific field exist in another list new value should be 2.

here is my code:

      data = [['ann', 10], ['nick', 15], ['juli', 14]] 
        final = pd.DataFrame(data, columns = ['index', 'Age']) 
        final['reward']=0
        list1=['ann']
        list2=['sam','nick']



 def rew(df):
           for i in df['value']:
                if i in list1:
                    df['reward']=1
                if i in list2:
                    df['reward']=2
                return df['reward']
    final['reward']=final.apply (rew,axis=1)
        expected result is:
    index   Age reward
    ann     10  1
    nick    15  2
    juli    14  0

Please,suggest where I can be wrong

Tony Z.
  • 101
  • 6
  • does this help?: https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column, and use `isin()` : https://stackoverflow.com/questions/19960077/how-to-implement-in-and-not-in-for-pandas-dataframe – anky Jun 04 '19 at 17:06
  • can you post a sample df and sample list along with the sample df? everyone will understand better whats required. – anky Jun 04 '19 at 17:09
  • Unfortunately no, cause I have two lists fot two different values – Tony Z. Jun 04 '19 at 17:09
  • Sure, will update now – Tony Z. Jun 04 '19 at 17:10

0 Answers0