我想在以下语句为真的“分数”列的每一行中添加“ 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)]
答案 0 :(得分:1)
您在那儿。只需使用df.loc[mask, "Score"] = 1
:
import numpy as np
import pandas as pd
df = pd.DataFrame({"Day7":np.random.rand(5)*1000,
"Score": np.random.rand(5)})
print(df)
df.loc[(df.Day7>=500), "Score"] = 1
print(df)
答案 1 :(得分:1)
请您尝试以下。
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
答案 2 :(得分:0)
df = df.assign(Score=0)
df.Score = df.Day7 >= 500