连接新的观察结果-DataFrame



我正在尝试连接新的观察结果。我得到的答案是,我认为这是正确的,但系统仍然回复我说"ValueError只能比较标签相同的DataFrame对象"有人能告诉我为什么在我认为我得到了正确的结果时会出现值错误吗?

问题是:

假设数据框架Employee如下:

Department      Title  Year Education Sex
Name                                           
Bob           IT    analyst     1  Bachelor   M
Sam        Trade  associate     3       PHD   M
Peter         HR         VP     8    Master   M
Jake          IT    analyst     2    Master   M

另一个数据帧new_observations是:

Department Education Sex      Title  Year
Mary             IT             F         VP   9.0
Amy               ?       PHD   F  associate   5.0
Jennifer      Trade    Master   F  associate   NaN
John             HR    Master   M    analyst   2.0
Judy             HR  Bachelor   F    analyst   2.0

用这些新的观察结果更新员工。

这是我的代码:

import pandas as pd
Employee =pd.DataFrame({"Name":["Bob","Sam","Peter","Jake"],
"Education":["Bachelor","PHD","Master","Master"],
"Sex":["M","M","M","M"],
"Year":[1,3,8,2],
"Department":["IT","Trade","HR","IT"],
"Title":["analyst", "associate", "VP", "analyst"]})
Employee=Employee.set_index('Name')
new_observations = pd.DataFrame({
"Name": ["Mary","Amy","Jennifer","John","Judy"],
"Department":["IT","?","Trade","HR","HR"],
"Education":["","PHD","Master","Master","Bachelor"],
"Sex":["F","F","F","M","F"],
"Title":["VP","associate","associate","analyst","analyst"],
"Year":[9.0,5.0,"NaN",2.0,2.0]},
columns= 
["Name","Department","Education","Sex","Title","Year"])
new_observations=new_observations.set_index('Name')
Employee = Employee.append(new_observations,sort=False)

这是我的结果:

代码结果

我也试过

Employee = pd.concat([Employee, new_observations], axis = 1, sort=False)

axis=0上使用pd.concat,这是默认值,因此不需要包含轴:

pd.concat([Employee, new_observations], sort=False)

输出:

Education Sex Year Department      Title
Name                                             
Bob       Bachelor   M    1         IT    analyst
Sam            PHD   M    3      Trade  associate
Peter       Master   M    8         HR         VP
Jake        Master   M    2         IT    analyst
Mary                 F    9         IT         VP
Amy            PHD   F    5          ?  associate
Jennifer    Master   F  NaN      Trade  associate
John        Master   M    2         HR    analyst
Judy      Bachelor   F    2         HR    analyst

最新更新