'int' object is not subscriptable after if statement

后端 未结 3 2050
终归单人心
终归单人心 2021-01-23 02:45

So I have a dataframe:

import pandas as pd

df = pd.DataFrame({\'name\': [\'Jason\', \'Molly\', \'Tina\', \'Jake\', \'Amy\'], 
        \'score\': [1, 3, 4, 5, 2]         


        
3条回答
  •  故里飘歌
    2021-01-23 02:52

    The problem is that apply applies your function to every single value in the column. df is not a DataFrame inside of are_you_ok, but (in your case) an integer. Naturally, Python is complaining that you cannot index into integers with ['happiness'].

    Your code is quite easy to fix, though. Just rewrite are_you_ok such that it works with integer arguments.

    In [1]: import pandas as pd
    In [2]: df = pd.DataFrame({'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
       ...:         'score': [1, 3, 4, 5, 2]})
       ...:         
    In [3]: def are_you_ok(x):
       ...:     if x >= 4:
       ...:         return 'happy'
       ...:     elif x <= 2:
       ...:         return 'sad'
       ...:     else:
       ...:         return 'ok'
       ...:     
    In [4]: df['happiness'] = df['score'].apply(are_you_ok)
    In [5]: df
    Out[5]: 
        name  score happiness
    0  Jason      1       sad
    1  Molly      3        ok
    2   Tina      4     happy
    3   Jake      5     happy
    4    Amy      2       sad
    

提交回复
热议问题