读取CSV文件,选择两列数据作为坐标点,计算任意两点的距离

import sys
import os
import pandas as pd
from math import sqrt
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QFileDialog, QComboBox, QPlainTextEdit, \
    QWidget, QHBoxLayout, QVBoxLayout


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("CSV文件两点距离计算")
        self.setGeometry(100, 100, 600, 400)

        self.file_layout = QHBoxLayout()

        self.button_select_file = QPushButton("选择文件", self)
        self.file_layout.addWidget(self.button_select_file)
        self.button_select_file.clicked.connect(self.select_file)

        self.label_selected_file = QLabel("", self)
        self.file_layout.addWidget(self.label_selected_file)

        self.combo_layout = QHBoxLayout()

        self.combo_x_column = QComboBox(self)
        self.combo_layout.addWidget(self.combo_x_column)

        self.combo_y_column = QComboBox(self)
        self.combo_layout.addWidget(self.combo_y_column)

        self.button_calculate = QPushButton("计算", self)
        self.combo_layout.addWidget(self.button_calculate)
        self.button_calculate.clicked.connect(self.calculate_distances)

        self.main_layout = QVBoxLayout()
        self.main_layout.addLayout(self.file_layout)
        self.main_layout.addLayout(self.combo_layout)

        self.label_length = QLabel(self)
        self.main_layout.addWidget(self.label_length)

        self.label_max = QLabel(self)
        self.main_layout.addWidget(self.label_max)

        self.textedit_distances = QPlainTextEdit(self)
        self.main_layout.addWidget(self.textedit_distances)

        central_widget = QWidget(self)
        central_widget.setLayout(self.main_layout)
        self.setCentralWidget(central_widget)

        self.selected_file = ""
        self.data = None

    def select_file(self):
        file_dialog = QFileDialog()
        self.selected_file, _ = file_dialog.getOpenFileName(self, "选择CSV文件", "", "CSV Files (*.csv)")

        file_name = os.path.basename(self.selected_file)  # 提取文件名(不含路径)
        self.label_selected_file.setText("选择的文件:" + file_name)

        # 清空之前的选项
        self.combo_x_column.clear()
        self.combo_y_column.clear()

        # 读取CSV文件并获取列名
        self.data = pd.read_csv(self.selected_file)
        columns = self.data.columns.tolist()

        # 添加列名到下拉框选项中
        self.combo_x_column.addItems(columns)
        self.combo_y_column.addItems(columns)

    def calculate_distances(self):
        if self.data is not None:
            # 获取选择的列索引
            x_column_index = self.combo_x_column.currentIndex() - 1
            y_column_index = self.combo_y_column.currentIndex() - 1

            # 获取选择的列数据
            x_data = self.data.iloc[:, x_column_index]
            y_data = self.data.iloc[:, y_column_index]

            # 计算每组元组与其余元组的距离,并存储到列表中
            distances = []
            for i in range(len(x_data)):
                for j in range(i + 1, len(x_data)):
                    distance = sqrt((x_data[i] - x_data[j]) ** 2 + (y_data[i] - y_data[j]) ** 2)
                    distances.append(distance)

            # 显示列表的长度
            self.label_length.setText("列表长度:" + str(len(distances)))

            # 显示列表的最大值
            max_value = max(distances)
            self.label_max.setText("列表最大值:" + str(max_value))

            # 将列表中的数据设置到文本框中,每个数据一行
            self.textedit_distances.setPlainText("\n".join(str(d) for d in distances))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值