简体   繁体   English

尝试合并两个不同数据框的两列时出现问题?

[英]Problem while trying to merge two columns of two different dataframes?

I am currently facing a problem that I don't seem to be able to solve with regards to handling and manipulating dataframes using Pandas.在使用 Pandas 处理和操作数据帧方面,我目前面临一个似乎无法解决的问题。

To give you an idea of the dataframes I'm talking about and that you'll see in my code:为了让您了解我正在谈论的数据框,您将在我的代码中看到:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

I'm trying to change the words found in column 'exercise' of the dataset 'data' with the words found in column 'name' of the dataset 'exercise'.我正在尝试将数据集“数据”的“锻炼”列中的单词更改为数据集“练习”的“名称”列中的单词。

For example, the acronym 'Dl' in the exercise column of the 'data' dataset should be changed into 'Dead lifts' found in the 'name' column of the 'exercise' dataset.例如,“数据”数据集的练习列中的首字母缩写词“Dl”应更改为“练习”数据集的“名称”列中的“Dead lifts”。

I have tried many methods but all have seemed to fail.我尝试了很多方法,但似乎都失败了。 I receive the same error every time.我每次都收到相同的错误。

Here is my code with the methods I tried:这是我尝试过的方法的代码:

### Method 1 ###

# Rename Name Column in 'exercise'
exercise = exercise.rename(columns={'label': 'exercise'})

# Merge Exercise Columns in 'exercise' and in 'data'
data = pd.merge(data, exercise, how = 'left', on='exercise')

### Method 2 ###
data.merge(exercise, left_on='exercise', right_on='label')

### Method 3 ###

data['exercise'] = data['exercise'].astype('category')
EXERCISELIST = exercise['name'].copy().to_list()
data['exercise'].cat.rename_categories(new_categories = EXERCISELIST, inplace = True)
                
### Same Error, New dataset ###

# Rename Name Column in 'area'
area = area.rename(columns={'description': 'area'})

# Merge Exercise Columns in 'exercise' and in 'data'
data = pd.merge(data, area, how = 'left', on = 'area')

This is the error I get:这是我得到的错误:

Traceback (most recent call last):回溯(最近一次通话最后):

File "---", line 232, in文件“---”,第 232 行,在
data.to_frame().merge(exercise, left_on='exercise', right_on='label') data.to_frame().merge(练习, left_on='练习', right_on='label')

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py", line 8192, in merge文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py”,第 8192 行,合并
return merge(返回合并(

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py", line 74, in merge文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py”,第 74 行,合并
op = _MergeOperation( op = _MergeOperation(

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py", line 668, in init文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py”,第 668 行,在init
) = self._get_merge_keys() ) = self._get_merge_keys()

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py", line 1046, in _get_merge_keys _get_merge_keys 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/reshape/merge.py”,第 1046 行
left_keys.append(left._get_label_or_level_values(lk)) left_keys.append(left._get_label_or_level_values(lk))

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py", line 1683, in _get_label_or_level_values _get_label_or_level_values 中的文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py”,第 1683 行
raise KeyError(key)引发 KeyError(键)

KeyError: 'exercise'键错误:'锻炼'

Is someone able to help me with this?有人能帮我解决这个问题吗? Thank you very much in advance.非常感谢您提前。

  1. merge, then drop and rename columns between data and area合并,然后在数据区域之间删除和重命名列
  2. merge, then drop and rename columns between step 1 and exercise合并,然后在步骤 1 和练习之间删除和重命名列
area = pd.DataFrame({"arealabel":["AGI","BAL"],
                    "description":["Agility","Balance"]})
exercise = pd.DataFrame({"description":["Jump rope","Dead lifts"],
                        "label":["Jr","Dl"]})
data = pd.DataFrame({"exercise":["Dl","Dl"],
                    "area":["AGI","BAL"],
                    "level":[0,3]})

(data.merge(area, left_on="area", right_on="arealabel")
 .drop(columns=["arealabel","area"])
 .rename(columns={"description":"area"})
 .merge(exercise, left_on="exercise", right_on="label")
 .drop(columns=["exercise","label"])
 .rename(columns={"description":"exercise"})
)
level等级 area区域 exercise锻炼
0 0 0 0 Agility敏捷 Dead lifts硬拉
1 1 3 3 Balance平衡 Dead lifts硬拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM