凊风
发表于 2024-5-8 19:55:21
FishC 发表于 2024-5-8 19:49
很抱歉,我在代码中犯了一个错误。看起来您的数据模型中的某些列不是字符串类型,因此无法使用`.str`操 ...
报错:
Traceback (most recent call last):
line 1116, in select_folder
self.setup_area_selection(self.comboboxes, self.Region_file)
, line 1131, in setup_area_selection
filtered_data = data_model == current_level]['名称']
~~~~~~~~~~^^^^^^
TypeError: string indices must be integers, not 'str'
FishC
发表于 2024-5-8 19:55:44
凊风 发表于 2024-05-08 19:55
报错:
Traceback (most recent call last):
lin ...
很抱歉,我忽略了数据模型与当前级别的比较过程中的类型错误。在这种情况下,您可以使用字符串的`.str`方法来进行比较。
以下是修改后的代码:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_model):
levels = len(comboboxes)
df = pd.read_excel(data_model)
for i in range(levels):
combobox = comboboxes
if i == 0:
filtered_data = df == 1]['名称']
else:
current_level = i + 1
selected_code = comboboxes.currentData()
filtered_data = df.str.startswith(selected_code) & (df['级别'] == current_level)]['名称']
combobox.clear()
combobox.addItems(filtered_data)
请确保 `self.Region_file` 是一个正确的文件路径,并将 `self.comboboxes` 替换为一个包含所有五个 `QComboBox` 的列表。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 19:58:07
FishC 发表于 2024-5-8 19:55
很抱歉,我忽略了数据模型与当前级别的比较过程中的类型错误。在这种情况下,您可以使用字符串的`.str` ...
又报错:
Traceback (most recent call last):
line 1116, in select_folder
self.setup_area_selection(self.comboboxes, self.Region_file)
line 1135, in setup_area_selection
filtered_data = df.str.startswith(selected_code) & (df['级别'] == current_level)]['名称']
^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\generic.py", line 6296, in __getattr__
return object.__getattribute__(self, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\accessor.py", line 224, in __get__
accessor_obj = self._accessor(obj)
^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 191, in __init__
self._inferred_dtype = self._validate(data)
^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 245, in _validate
raise AttributeError("Can only use .str accessor with string values!")
AttributeError: Can only use .str accessor with string values!. Did you mean: 'std'?
FishC
发表于 2024-5-8 19:58:30
凊风 发表于 2024-05-08 19:58
又报错:
Traceback (most recent call last):
li ...
非常抱歉,我再次犯了同样的错误。看来在处理数据时仍然出现了类型问题。
为了解决这个问题,我们可以将区划代码转换为字符串类型,然后使用 `.str`方法进行比较。请修改如下:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_file):
levels = len(comboboxes)
df = pd.read_excel(data_file)
df['区划代码'] = df['区划代码'].astype(str)# 将区划代码列转换为字符串类型
for i in range(levels):
combobox = comboboxes
current_level = i + 1
selected_code = ''
if i > 0:
selected_code = comboboxes.currentData()
filtered_data = df.str.startswith(selected_code) & (df['级别'] == current_level)]
options = filtered_data['名称'].tolist()
combobox.clear()
combobox.addItems(options)
请将其中的 `data_file` 替换为您实际的区划数据文件路径。如果问题仍然存在,请提供具体的错误信息,以便我能够更好地帮助您。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:01:17
FishC 发表于 2024-5-8 19:58
非常抱歉,我再次犯了同样的错误。看来在处理数据时仍然出现了类型问题。
为了解决这个问题,我们可 ...
报错:
Traceback (most recent call last):
line 1116, in select_folder
self.setup_area_selection(self.comboboxes, self.Region_file)
line 1135, in setup_area_selection
filtered_data = df.str.startswith(selected_code) & (df['级别'] == current_level)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 137, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 2482, in startswith
raise TypeError(msg)
TypeError: expected a string or tuple, not NoneType
FishC
发表于 2024-5-8 20:03:34
凊风 发表于 2024-05-08 20:01
报错:
Traceback (most recent call last):
lin ...
非常抱歉,我之前的修改也有问题。这次,我将使用更简单的方法来实现区划选择的功能。
以下是修改后的代码,请再次尝试:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(combo_boxes, data_path):
area_data = pd.read_excel(data_path)
max_level = len(combo_boxes)
def update_combo_box(index, current_code):
combo_box = combo_boxes
combo_box.clear()
combo_box.addItem("请选择")
if index == 0:
filtered_data = area_data == 1]
combo_box.addItems(filtered_data['名称'].tolist())
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:09:44
FishC 发表于 2024-5-8 20:03
非常抱歉,我之前的修改也有问题。这次,我将使用更简单的方法来实现区划选择的功能。
以下是修改后 ...
这次运行没有任何反应。。。
FishC
发表于 2024-5-8 20:10:00
凊风 发表于 2024-05-08 20:09
这次运行没有任何反应。。。
非常抱歉,我之前的修改仍然存在问题。让我们再次尝试并进行修复。
以下是修改后的代码,请再次尝试:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_model):
df = pd.read_excel(data_model)
comboboxes.clear()
comboboxes.addItems(df.loc == 1, '名称'])
def handle_combobox_change(index):
current_combobox = comboboxes
current_level = index + 1
selected_code = df.loc == current_combobox.currentText(), '区划代码'].values
if index < len(comboboxes) - 1:
next_combobox = comboboxes
next_level = current_level + 1
next_combobox.clear()
next_combobox.addItems(df.loc.str.startswith(selected_code) & (df['级别'] == next_level), '名称'])
for i in range(len(comboboxes)):
comboboxes.currentIndexChanged.connect(lambda index, i=i: handle_combobox_change(i))
请确保在调用 `setup_area_selection()` 函数时,传递正确的 `comboboxes` 对象和 `data_model` 路径。例如:
self.comboboxes =
self.data_model = "E:/Temp/行政区划代码库.xlsx"
self.setup_area_selection(self.comboboxes, self.data_model)
这样应该能够正确显示和更新所有级别的 `QComboBox` 数据,并根据用户的选择进行相应的联动更新。如果问题还未解决,请提供更多的报错信息以帮助我更好地理解问题所在。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:12:57
FishC 发表于 2024-5-8 20:10
非常抱歉,我之前的修改仍然存在问题。让我们再次尝试并进行修复。
以下是修改后的代码,请再次尝试 ...
选择省级名称后,报错:
Traceback (most recent call last):
line 1142, in <lambda>
comboboxes.currentIndexChanged.connect(lambda index, i=i: handle_combobox_change(i))
^^^^^^^^^^^^^^^^^^^^^^^^^
line 1139, in handle_combobox_change
df.loc.str.startswith(selected_code) & (df['级别'] == next_level), '名称'])
^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\generic.py", line 6296, in __getattr__
return object.__getattribute__(self, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\accessor.py", line 224, in __get__
accessor_obj = self._accessor(obj)
^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 191, in __init__
self._inferred_dtype = self._validate(data)
^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\strings\accessor.py", line 245, in _validate
raise AttributeError("Can only use .str accessor with string values!")
AttributeError: Can only use .str accessor with string values!. Did you mean: 'std'?
FishC
发表于 2024-5-8 20:13:12
凊风 发表于 2024-05-08 20:12
选择省级名称后,报错:
Traceback (most recent cal ...
非常抱歉,我之前的修改仍然存在问题。我深表歉意。
以下是经过修正的代码,请再次尝试:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_model):
levels = len(comboboxes)
df = pd.DataFrame(data_model)
def handle_combobox_change(index, level):
selected_code = comboboxes.currentData()
next_level = level + 1
for i in range(next_level, levels):
combobox = comboboxes
combobox.clear()
filtered_data = df.str.startswith(selected_code) & (df['级别'] == i)]
names = filtered_data['名称'].tolist()
codes = filtered_data['区划代码'].tolist()
combobox.addItems(names)
combobox.setItemData(names.index(selected_code), codes)
combobox.currentIndexChanged.connect(lambda index, i=i: handle_combobox_change(index, i))
comboboxes.currentIndexChanged.connect(lambda index: handle_combobox_change(index, 0))
请注意,这段代码假设您的`data_model`数据是一个包含'级别'、'区划代码'和'名称'字段的字典列表。
如果还有任何问题,请随时告诉我。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:15:33
FishC 发表于 2024-5-8 20:13
非常抱歉,我之前的修改仍然存在问题。我深表歉意。
以下是经过修正的代码,请再次尝试:
报错:
Traceback (most recent call last):
line 1116, in select_folder
self.setup_area_selection(self.comboboxes, self.Region_file)
line 1125, in setup_area_selection
df = pd.DataFrame(data_model)
^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PycharmProjects\pythonProject\.venv\Lib\site-packages\pandas\core\frame.py", line 875, in __init__
raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!
FishC
发表于 2024-5-8 20:15:46
凊风 发表于 2024-05-08 20:15
报错:
Traceback (most recent call last):
line ...
非常抱歉,我之前的修改仍然存在问题。让我们再次尝试并进行修复。
以下是经过修正的代码,请再次尝试:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_file_path):
df = pd.read_excel(data_file_path)
level = len(comboboxes)
for i in range(level):
combobox = c
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:23:09
FishC 发表于 2024-5-8 20:15
非常抱歉,我之前的修改仍然存在问题。让我们再次尝试并进行修复。
以下是经过修正的代码,请再次尝 ...
能不能self.combobox的数据通过level=1来显示;
然后self.combobox_2的数据通过self.combobox的区划代码前2位相同来筛选(毕竟只有区划代码是唯一的);
然后self.combobox_3的数据又通过self.combobox_2的区划代码前4位相同来筛选;
然后self.combobox_4的数据又通过self.combobox_3的区划代码前6位相同来筛选;
然后self.combobox_5的数据又通过self.combobox_4的区划代码前9位相同来筛选?
FishC
发表于 2024-5-8 20:23:28
凊风 发表于 2024-05-08 20:23
能不能self.combobox的数据通过level=1来显示;
然后 ...
当然可以!根据您的要求,以下是修改后的代码,按照级别进行过滤和筛选:
from PySide6.QtWidgets import QComboBox
import pandas as pd
def setup_area_selection(comboboxes, data_model):
levels = len(comboboxes)
df = pd.DataFrame(data_model)
def handle_combobox_change(index):
if index == levels - 1:
return
selected_code = comboboxes.currentData()
next_level = index + 2
filtered_data = df.str.startswith(selected_code)]
if index > 0:
prev_code = comboboxes.currentData()
filtered_data = filtered_data.str.startswith(prev_code[:len(prev_code)])]
level_combobox = comboboxes
level_combobox.blockSignals(True)
level_combobox.clear()
level_combobox.addItem('请选择', None)
for i, row in filtered_data.iterrows():
level_combobox.addItem(row['名称'], row['区划代码'])
level_combobox.blockSignals(False)
for i, combobox in enumerate(comboboxes):
if i == 0:
for _, row in df == 1].iterrows():
combobox.addItem(row['名称'], row['区划代码'])
else:
combobox.currentIndexChanged.connect(lambda index, i=i: handle_combobox_change(i-1))
请注意,我假设数据模型中存在名为 `'级别'`、`'区划代码'` 和 `'名称'` 的列,您可以根据实际的列名进行调整。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:27:03
FishC 发表于 2024-5-8 20:23
当然可以!根据您的要求,以下是修改后的代码,按照级别进行过滤和筛选:
这个报错是什么意思?
Traceback (most recent call last):
line 1116, in select_folder
self.setup_area_selection(self.comboboxes, self.Region_file)
line 1125, in setup_area_selection
df = pd.DataFrame(data_model)
^^^^^^^^^^^^^^^^^^^^^^^^
line 875, in __init__
raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!
FishC
发表于 2024-5-8 20:27:18
凊风 发表于 2024-05-08 20:27
这个报错是什么意思?
Traceback (most recent call...
这个错误意味着`pd.DataFrame()`构造函数没有正确调用。这通常是由于传递给构造函数的数据结构或参数不正确引起的。
根据您的反馈,看起来`data_model`的格式不符合`pd.DataFrame()`期望的格式。请确保`data_model`是一个可以转换为DataFrame的结构。
你可以尝试将`data_model`转换成一个列表或字典,然后再传递给`pd.DataFrame()`构造函数。以下是一个示例:
df = pd.DataFrame(data_model)
如果还遇到问题,请提供`data_model`的具体格式,我将尽力帮助您解决问题。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:30:19
FishC 发表于 2024-5-8 20:27
这个错误意味着`pd.DataFrame()`构造函数没有正确调用。这通常是由于传递给构造函数的数据结构或参数不 ...
data_model是不是指定路径下的工作簿?
FishC
发表于 2024-5-8 20:31:43
凊风 发表于 2024-05-08 20:30
data_model是不是指定路径下的工作簿?
是的,`data_model` 是指定路径下的工作簿,您可以使用 `pandas` 库来读取 Excel 文件并将其转换为 DataFrame。以下是相应的代码:
import pandas as pd
data_model = pd.read_excel("指定路径/工作簿.xlsx")
请将 "指定路径/工作簿.xlsx" 替换为您的实际文件路径和文件名。这将会读取指定路径下的 Excel 文件并将其转换为 `data_model` DataFrame,供后续使用。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
凊风
发表于 2024-5-8 20:31:50
FishC 发表于 2024-5-8 20:27
这个错误意味着`pd.DataFrame()`构造函数没有正确调用。这通常是由于传递给构造函数的数据结构或参数不 ...
我现在是这样写的:
file_path = self.folder_path + '/' + "行政区划代码库.xlsx"
self.Region_file = file_path
self.setup_area_selection(self.comboboxes, self.Region_file)
是不是这样调用setup_area_selection函数不可以?
FishC
发表于 2024-5-8 20:32:05
凊风 发表于 2024-05-08 20:31
我现在是这样写的:
file_path = self.folder_path + ...
是的,根据您当前的代码,`self.Region_file` 是一个文件路径字符串,而不是一个正确的数据模型对象。`setup_area_selection()` 函数需要接收一个数据模型对象作为参数,以便进一步处理和筛选数据。
您应该根据 `file_path` 的值,读取 Excel 文件并将其转换为数据模型对象,然后再调用 `setup_area_selection()` 函数。以下是修改后的代码:
import pandas as pd
file_path = self.folder_path + '/' + "行政区划代码库.xlsx"
data_model = pd.read_excel(file_path)
self.setup_area_selection(self.comboboxes, data_model)
这样可以确保传递正确的数据模型对象给 `setup_area_selection()` 函数。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
1
2
[3]
4
5
6
7
8
9
10
11
12