采用 loc[] 方法多适用于对空的dataframe循环遍历添加行,这样索引可以从0开始直到数据结果,不会存在索引冲突的问题。
不过在使用insert的过程中发现 454: DeprecationWarning: `input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`. status, indent_spaces = self.shell.input_splitter.check_complete(code) 这个提示,猜测是有别的地方出问题了,还需要调试。
主要参考资料:
先说原因:三条语句按顺序运行,肯定以最后一句结果为准,所以你感觉只有最后一行生效。
一个建议:取样本测试数据的时候,考虑下取方便显示验证的数据。
解决方案:如果之前tag内容,直接保留tag内容。代码如下变动
my_dict = {'keyword': [False, False, False, True], 'sampling': [False, True, False, False],
'value': [True, False, False, False], 'Tag': ['No Tag', 'No Tag', 'No Tag', 'No Tag']}
result_fin_copy = pd.DataFrame(my_dict)
print(result_fin_copy)
result_fin_copy['Tag'] = result_fin_copy.apply(lambda x: 'No Tag' if x.value != True else 'Score', axis=1)
result_fin_copy['Tag'] = result_fin_copy.apply(lambda x: x.Tag if x.sampling != True else 'Sampled', axis=1)
result_fin_copy['Tag'] = result_fin_copy.apply(lambda x: x.Tag if x.keyword != True else 'Keyword', axis=1)
print(result_fin_copy)
运行结果:
keyword sampling value Tag
0 False False True No Tag
1 False True False No Tag
2 False False False No Tag
3 True False False No Tag
keyword sampling value Tag
0 False False True Score
1 False True False Sampled
2 False False False No Tag
3 True False False Keyword
Process finished with exit code 0
后记,这个代码还是有缺陷的,或者说这个样本数据本身的逻辑还有点问题,即,如果一行同时满足两个条件,应该显示哪个Tag。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)