gpt4 book ai didi

python - 如何修复我的 pandas 数据框中的索引,使其不只将值保持为零,而是增加值?

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:37 24 4
gpt4 key购买 nike

目前,我正在 Python 脚本中比较两个图像的关键点。如果我运行 cv2 比较脚本,比较结果将存储在 Pandas 数据框中。在此数据框中,索引将值保持为零 (0),并且在每次运行脚本后都不会增加。

请看下面的结果:

Key_points_1        Key_points_2        Percentage      Result
0 2737 2709 0.84 Fail
0 2737 2709 0.84 Fail
0 2737 3283 25.16 Pass
0 2737 3283 25.16 Pass
# Create pandas dataframe with the data from comparison.

initial_data = {'Key_points_1': [int(len(kp_1))],
'Key_points_2': [int(len(kp_2))],
'Percentage': [percentage]}

df = pd.DataFrame(initial_data, columns=['Key_points_1', 'Key_points_2', 'Percentage'])

result = []
for value in df["Percentage"]:
if value >= 15:
result.append("Pass") # Threshold to be defined.
else:
result.append("Fail") # Threshold to be defined.

df["Result"] = result

df.to_csv('output/cvs_DataFrame.csv', sep='\t', encoding='utf-8', mode='a', header=False)

想要的结果应该是这样的:

Index   Key_points_1        Key_points_2        Percentage      Result
0 2737 2709 0.84 Fail
1 2737 2709 0.84 Fail
2 2737 3283 25.16 Pass
3 2737 3283 25.16 Pass

最佳答案

更好的是使用DataFrame.reset_indexnumpy.where而不是你的循环解决方案:

df = df.reset_index(drop=True)
df["Result"] = np.where(df["Percentage"] >= 15,"Pass", "Fail")
print (df)
Key_points_1 Key_points_2 Percentage Result
0 2737 2709 0.84 Fail
1 2737 2709 0.84 Fail
2 2737 3283 25.16 Pass
3 2737 3283 25.16 Pass

关于python - 如何修复我的 pandas 数据框中的索引,使其不只将值保持为零,而是增加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com