Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何连接两个d.f类键和值_Python 3.x_Pandas_Join_Concat - Fatal编程技术网

Python 3.x 如何连接两个d.f类键和值

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

我有两个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  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