Python 如果未找到数据框列,则替换列值
我是python新手。这里我有下面的dataframe列Python 如果未找到数据框列,则替换列值,python,python-3.x,pandas,numpy,Python,Python 3.x,Pandas,Numpy,我是python新手。这里我有下面的dataframe列 Predict 100 200 2100 2200 51200 0 3600 现在我有以下几点 数组数据 cols = [100,156,160,162,200,256,262,2200,2600,2900,3600,4600] 现在,如果它不在预测中,我尝试用0替换它 所以结果会是这样的 predict 100 200 0 2200 0 3600 现在我试过了 co
Predict
100
200
2100
2200
51200
0
3600
现在我有以下几点
数组数据
cols = [100,156,160,162,200,256,262,2200,2600,2900,3600,4600]
现在,如果它不在预测中,我尝试用0
替换它
所以结果会是这样的
predict
100
200
0
2200
0
3600
现在我试过了
compare_df[~compare_df.isin(cols)] = 0
但我得到了这个错误
TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value
有人能帮我吗?谢谢。您必须使用
系列
而不是一列数据帧
,方法是选择with column name和loc
替换Predict
的值:
compare_df.loc[~compare_df['Predict'].isin(cols), 'Predict'] = 0
compare_df['Predict'] = np.where(compare_df['Predict'].isin(cols),compare_df['Predict'], 0)
如果使用列名删除loc
,则通过掩码将所有行设置为0
(如果存在):
如果将altarative与选择列一起使用,请选择列Predict
:
compare_df.loc[~compare_df['Predict'].isin(cols), 'Predict'] = 0
compare_df['Predict'] = np.where(compare_df['Predict'].isin(cols),compare_df['Predict'], 0)
但在这方面也有工作:
compare_df['Predict'] = np.where(compare_df.isin(cols),compare_df, 0)
编辑:
对于比较,需要在列和列表中使用相同的类型,例如数字或对象(显然是字符串)
因此,这两个字符串值都是必需的:
cols = [str(x) for x in cols]
compare_df.loc[~compare_df['Predict'].isin(cols), 'Predict'] = 0
或对于这两个数字:
compare_df['Predict'] = compare_df['Predict'].astype(float)
compare_df.loc[~compare_df['Predict'].isin(cols), 'Predict'] = 0
如果不工作,则通过.astype(float)
:
这是。
它比np.where
更好,因为只有当值不在cols中时,才需要赋值0
new_df=df.where(df.isin(cols),0)
print(new_df)
如果有多个列:
new_df=df.copy()
new_df['Predict']=df['Predict'].where(df['Predict'].isin(cols),0)
print(new_df)
Predict
0 100
1 200
2 0
3 2200
4 0
5 0
6 3600
如果它们有不同的类型:
new_df=df.copy()
new_df['Predict']=new_df['Predict'].astype(str) #this or the commented line depending on the type of cols and df ['Predict']
#new_df['predict']=new_df['Predict'].astype(int)
new_df['Predict']=df['Predict'].where(df['Predict'].isin(cols),0)
print(new_df)
这里所有的东西都是0,即使它是present@ganeshkaspate-什么是
打印(比较_df['Predict'].dtype)
?只有当该列值不存在于该数组中时,它才应该存在,否则,只有它给出的列值才应该存在object@ganeshkaspate-这意味着您将字符串与整数与列表进行比较。可能存在重复的
new_df=df.copy()
new_df['Predict']=new_df['Predict'].astype(str) #this or the commented line depending on the type of cols and df ['Predict']
#new_df['predict']=new_df['Predict'].astype(int)
new_df['Predict']=df['Predict'].where(df['Predict'].isin(cols),0)
print(new_df)