求指导
# coding=utf-8# import pandas as pd
# may_df = pd.read_csv('may.csv')
# june_df = pd.read_csv('june.csv')
#
# merged = pd.merge(may_df,june_df,on="states",how = 'outer',suffixes=('_ahead','behind'),indicator=True)
# # find change
# changed_rows = merged!='both']
# # find new lines
# new_rows = merged=='right_only']
# print(new_rows)
#
import pandas as pd
from tqdm import tqdm
# # 读取两张表
# df1 = pd.read_csv('may.csv')
# df2 = pd.read_csv('june.csv')
#
# # 筛选出location、project_name、building、unit、high和room相同的行
# cols = ['location', 'project_name', 'building', 'unit', 'high', 'room']
# df1_grouped = df1.groupby(cols).first().reset_index()
# df2_grouped = df2.groupby(cols).first().reset_index()
#
# # 将df1_grouped和df2_grouped合并,并比较states列的值
# df = pd.DataFrame(columns=df1.columns)
# for index, row1 in tqdm(df1_grouped.iterrows(), total=len(df1_grouped)):
# for _, row2 in df2_grouped.iterrows():
# if row1['location'] == row2['location'] and \
# row1['project_name'] == row2['project_name'] and \
# row1['building'] == row2['building'] and \
# row1['unit'] == row2['unit'] and \
# row1['high'] == row2['high'] and \
# row1['room'] == row2['room'] and \
# row1['states'] != row2['states']:
# df = df.append(row1)
#
# # 输出结果
# print(df)
import pandas as pd
from tqdm import tqdm
# 读取两张表
df1 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\A.xlsx')
df2 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\B.xlsx')
# 筛选出location、project_name、building、unit、high和room相同的行 ,'got_date'
cols = ['备案名', '楼栋', '单元号', '房号', '面积', '清水价', '装修价', '物业类型']
df1_grouped = df1.groupby(cols).first().reset_index()
df2_grouped = df2.groupby(cols).first().reset_index()
# 合并两张表,并筛选出states有变化的行
df_merged = pd.merge(df1_grouped, df2_grouped, on=cols, suffixes=('_1', '_2'))
df = df_merged.loc != df_merged['颜色_2']]
df.to_excel('D:\pydata\data.xlsx', index=False)
# 输出结果
print(df)
D:\PythonProject\pythonProject\Scripts\python.exe "E:\qycache\xuexi\pythonProject\房地产\process(3).py"
D:\PythonProject\pythonProject\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\PythonProject\pythonProject\lib\site-packages\numpy\.libs\libopenblas.EL2C6PLE4ZYW3ECEVIV3OXXGRN2NRFM2.gfortran-win_amd64.dll
D:\PythonProject\pythonProject\lib\site-packages\numpy\.libs\libopenblas64__v0.3.21-gcc_10_3_0.dll
warnings.warn("loaded more than 1 DLL from .libs:"
Traceback (most recent call last):
File "E:\qycache\xuexi\pythonProject\房地产\process(3).py", line 55, in <module>
df_merged = pd.merge(df1_grouped, df2_grouped, on=cols, suffixes=('_1', '_2'))
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\reshape\merge.py", line 144, in merge
op = _MergeOperation(
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\reshape\merge.py", line 737, in __init__
self._maybe_coerce_merge_keys()
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\reshape\merge.py", line 1389, in _maybe_coerce_merge_keys
raise ValueError(msg)
ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat
进程已结束,退出代码1
这段代码的目的是将两个Excel文件中相同的行合并,并找出其中状态(states)发生变化的行。然而,代码中存在一些问题导致运行出错。
首先,代码中使用了 pd.read_csv 函数读取CSV文件,但实际上文件的格式是Excel文件,应该使用 pd.read_excel 函数来读取。你需要将代码中的 pd.read_csv 改为 pd.read_excel ,并将文件路径改为相应的Excel文件路径。
其次,代码中使用了 tqdm 库来显示进度条,但你的代码中并没有导入该库。如果你想要使用进度条,你需要安装 tqdm 库,并在代码开头添加 from tqdm import tqdm 。
最后,代码中进行合并的列名存在问题。在代码中,使用了 location 、 project_name 、 building 、 unit 、 high 和 room 作为合并的列名,但实际上这些列名在你的Excel文件中并不存在。你需要根据实际情况修改合并的列名,确保这些列名在你的Excel文件中存在。
修改后的代码如下:
import pandas as pd
from tqdm import tqdm
# 读取两张表
df1 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\A.xlsx')
df2 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\B.xlsx')
# 筛选出location、project_name、building、unit、high和room相同的行
cols = ['备案名', '楼栋', '单元号', '房号', '面积', '清水价', '装修价', '物业类型']
df1_grouped = df1.groupby(cols).first().reset_index()
df2_grouped = df2.groupby(cols).first().reset_index()
# 合并两张表,并筛选出states有变化的行
df_merged = pd.merge(df1_grouped, df2_grouped, on=cols, suffixes=('_1', '_2'))
df = df_merged.loc != df_merged['颜色_2']]
df.to_excel('D:\pydata\data.xlsx', index=False)
# 输出结果
print(df)
请根据你的实际情况修改代码中的文件路径和列名,并安装所需的库后再次尝试运行。 新手乐园
首先,你的代码在执行时遇到了一个错误。错误信息显示,在合并数据框时,有一些列的数据类型不匹配。具体来说,它提到你尝试在一个对象类型和一个float64类型的列上进行合并。为了解决这个问题,你可以使用pd.concat函数来合并数据框而不是使用pd.merge。
现在让我帮你修改一下代码,以解决这个问题:
import pandas as pd
# 读取两张表
df1 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\A.xlsx')
df2 = pd.read_excel('D:\一介书生资料库\爬虫:八爪鱼\各市县整体市场\shujuchuli\B.xlsx')
# 筛选出location、project_name、building、unit、high和room相同的行,并选择指定的列
cols = ['备案名', '楼栋', '单元号', '房号', '面积', '清水价', '装修价', '物业类型']
df1_grouped = df1.groupby(cols).first().reset_index()
df2_grouped = df2.groupby(cols).first().reset_index()
# 合并两张表,并筛选出states有变化的行
df_merged = pd.concat()
df_duplicates = df_merged
df_changed = df_duplicates.loc != df_duplicates['颜色_2']]
# 输出结果
df_changed.to_excel('D:\pydata\data.xlsx', index=False)
print(df_changed)
这个修改后的代码将使用pd.concat函数合并两张表,并根据指定的列筛选出存在变化的行。最后,它会将结果保存到Excel文件中,并打印输出。
请注意,你需要将文件路径修改为正确的路径,并确保你安装了所需的包(pandas)。
希望这能帮到你!如果有任何进一步的问题,请随时问我。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} isdkz 发表于 2023-11-1 15:25
这段代码的目的是将两个Excel文件中相同的行合并,并找出其中状态(states)发生变化的行。然而,代码中存 ...
你说的上面的都有都对更换列表名的时候错误执行原来的列表名是对的 Mike_python小 发表于 2023-11-1 15:27
新手乐园
首先,你的代码在执行时遇到了一个错误。错误信息显示,在合并数据框时,有一些列的数据类型不匹 ...
收到我也发现了原来自己的错误谢谢 Mike_python小 发表于 2023-11-1 15:27
新手乐园
首先,你的代码在执行时遇到了一个错误。错误信息显示,在合并数据框时,有一些列的数据类型不匹 ...
D:\PythonProject\pythonProject\Scripts\python.exe E:\qycache\xuexi\pythonProject\房地产\process(版本2.3).py
D:\PythonProject\pythonProject\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\PythonProject\pythonProject\lib\site-packages\numpy\.libs\libopenblas.EL2C6PLE4ZYW3ECEVIV3OXXGRN2NRFM2.gfortran-win_amd64.dll
D:\PythonProject\pythonProject\lib\site-packages\numpy\.libs\libopenblas64__v0.3.21-gcc_10_3_0.dll
warnings.warn("loaded more than 1 DLL from .libs:"
Traceback (most recent call last):
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\indexes\base.py", line 3652, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '状态_1'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\qycache\xuexi\pythonProject\房地产\process(版本2.3).py", line 57, in <module>
df_changed = df_duplicates.loc != df_duplicates['状态_2']]
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\PythonProject\pythonProject\lib\site-packages\pandas\core\indexes\base.py", line 3654, in get_loc
raise KeyError(key) from err
KeyError: '状态_1'
进程已结束,退出代码1
还是提示错误
页:
[1]