|
楼主 |
发表于 2024-4-10 21:37:07
|
显示全部楼层
日期 车牌_x 加油升数 车牌_y _merge
7 2024-03-11 京A00001 215.0 京B00002 both
8 2024-03-11 京B00002 215.0 京A00001 both
13 2024-03-11 京C00011 220.0 京D00220 both
14 2024-03-11 京D00220 220.0 京C00011 both
28 2024-03-14 京F10010 215.0 京G20020 both
31 2024-03-14 京G20020 215.0 京F10010 both
其他的都问题不大,就是这个找车牌不同的,因为加油的时候,两部车如果加油的日期和加油的升数刚好相等的话,它会出现两条记录,这种同一天,两部车加同样升数的油的情况还挺常见的,怎么解决这个问题
你给我的答案是说:
import pandas as pd
# 读取数据
data = {'日期': ['2024-03-11', '2024-03-11', '2024-03-11', '2024-03-11', '2024-03-14', '2024-03-14'],
'车牌_x': ['京A00001', '京B00002', '京C00011', '京D00220', '京F10010', '京G20020'],
'加油升数': [215.0, 215.0, 220.0, 220.0, 215.0, 215.0],
'车牌_y': ['京B00002', '京A00001', '京D00220', '京C00011', '京G20020', '京F10010']}
df = pd.DataFrame(data)
# 合并数据
merged_df = pd.merge(df, df, on=['日期', '加油升数'], suffixes=('_x', '_y'))
# 筛选出车牌不同但日期和加油升数相同的记录
diff_cars_same_date_and_volume = merged_df[(merged_df['车牌_x'] != merged_df['车牌_y'])]
print(diff_cars_same_date_and_volume)
但是执行到:merged_df = pd.merge(df, df, on=['日期', '加油升数'], suffixes=('_x', '_y')) 这里都还是正常的,但是这里来print(merged_df),它的列号就变成了下面这个样的了:
日期 车牌_x_x 加油升数 车牌_y_x 车牌_x_y 车牌_y_y
所以你再执行这个diff_cars_same_date_and_volume = merged_df[(merged_df['车牌_x'] != merged_df['车牌_y'])],,,它就会说找不到车牌_x
报了以下错误:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '车牌_x'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\CheckForm\Get_All_Sheet.py", line 105, in <module>
diff_cars_same_date_and_volume = merged_df[(merged_df['车牌_x'] != merged_df['车牌_y'])]
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: '车牌_x'
所以,,我要怎么解决这个问题 |
|