This question already has answers here:
Conditional Replace Pandas
                                
                                    (6个答案)
                                
                        
                                在11个月前关闭。
            
                    
我想在“ Score”列的每一行中添加“ 1”,其中以下语句为真,

import pandas as pd
import numpy as np


df = pd.read_csv(Path1 + 'Test.csv')
df.replace(np.nan, 0, inplace=True)
df[(df.Day7 >= 500)]


Sample Value

Output

最佳答案

您可以尝试以下吗?

df['score']=np.where(df['Day7']>=500,1,"")


或根据OP的评论(在此处添加@ anky_91的增强解决方案):

np.where((df['Day7']>=500)&(df['Day7']<1000),1,"")


当我们打印df的值时,将是输出。

    Cat          Day7    score
0   Advertisir   145
1   Blogs        56
2   Business     92
3   Classfied    23
4   Continuin    110
5   Corporate    1974     1

关于python - 如果条件匹配,如何增加值(value),python ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55206857/

10-09 09:03