哪里给了我很多错误,所以我正在寻找一个解决方案与df.loc代替。
这是np。我得到的错误是:
C:\Users\xxx\AppData\Local\Continuum\Anaconda2\lib\site-packages\ipykernel\__main__.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
我正在使用以下数据帧df:
df = pd.DataFrame({'Column_A': ['AAA','AAA','ABC','CDE'],'checked': ['0','0','1','0'],'duplicate': ['True','True','False','False']})
Column_A checked duplicate
0 AAA 0 True
1 AAA 0 True
2 ABC 1 False
3 CDE 0 False
我想创建一个额外的标志,如果选中是0和重复是真的。
我试过但没用:
df['flag'] = (np.where((df['checked'] == 'Y') &(df['duplicate'] == 'True'), 'Y', '0'))
TypeError: invalid type comparison
我和df.loc一起试过了:
df['flag'] = (df.loc[df['checked'] == 'Y']& df.loc[df['duplicate'] == 'True'], 'Y','0')
TypeError: invalid type comparison
我也犯了同样的错误!
我认为你的
boolean
不是string
,所以需要删除'
:或者如果与
boolean
列比较,可以省略== True
:如果需要检查
checked
需要'
,因为strings
:编辑:
带^{} 的解决方案:
相关问题 更多 >
编程相关推荐