Python 3.x 如何连接两个d.f类键和值
我有两个df,我使用python和pandas df1类似于一个键,因为df2有多个df1索引 我该如何连接(df1、df2)以获得此df? 我试着这么做Python 3.x 如何连接两个d.f类键和值,python-3.x,pandas,join,concat,Python 3.x,Pandas,Join,Concat,我有两个df,我使用python和pandas df1类似于一个键,因为df2有多个df1索引 我该如何连接(df1、df2)以获得此df? 我试着这么做 df1=pd.concat([df1,df2],axis=1,join='inner') 我得到ValueError:传递值的形状 #Use the join method for the new dataframe df_new=df2.join(df1) print(df_new) 输出: year color n
df1=pd.concat([df1,df2],axis=1,join='inner')
我得到ValueError:传递值的形状
#Use the join method for the new dataframe
df_new=df2.join(df1)
print(df_new)
输出:
year color number
names
albert 1774 blue NaN
alex 2034 red 30.0
dan 1990 red 25.0
dan 2000 red 25.0
david 1980 red NaN
tommy 1956 blue 28.0
tommy 1922 blue 28.0
请将数据帧作为文本而不是图像发布。在df1
中,与tommy
关联的数字是28。在带有tommy
的df_new
中,关联的数字为30
。你能解释一下背后的逻辑吗?亚历克斯的情况也是如此。您是否将数据帧写错了,或者它背后是否有任何逻辑?这不是预期的输出。
#Use the join method for the new dataframe
df_new=df2.join(df1)
print(df_new)
year color number
names
albert 1774 blue NaN
alex 2034 red 30.0
dan 1990 red 25.0
dan 2000 red 25.0
david 1980 red NaN
tommy 1956 blue 28.0
tommy 1922 blue 28.0