简体   繁体   中英

Add values into DataFrame Python 3.0

I'm trying to add values into the 'Overall' column using both if-else and for-loop, however it doesn't seem to work.

I'm using the values from the 'Total' Column of my DataFrame to determine the 'Overall'column value either a Pass or a Fail.]

    for index, row in Score.iterrows():
         if Score['Total']>=100:
            Score.insert(index,'Overall', 'Pass')
          else:
            Score.insert(index,'Overall', 'Fail')

        Name    Maths   English Total   Overall
    0   Tom        50   50      100 
    1   Jack       80   50      130 
    2   Tim        70   80      150 
    3   Hen         1   84      85  

Attached picture of the problem

在此处输入图片说明

Score['Overall'] = Score['Total'].map(lambda x: 'Pass' if x>=100 else 'Fail')

Consider using vectorized np.where() :

In [128]: Score
Out[128]:
   Name  Maths  English  Total
0   Tom     50       50    100
1  Jack     80       50    130
2   Tim     70       80    150
3   Hen      1       84     85

In [129]: Score['Overall'] = np.where(Score['Total'] >= 100, 'Pass', 'Fail')

In [130]: Score
Out[130]:
   Name  Maths  English  Total Overall
0   Tom     50       50    100    Pass
1  Jack     80       50    130    Pass
2   Tim     70       80    150    Pass
3   Hen      1       84     85    Fail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM