inaMinute
inaMinute

Reputation: 585

A strange errro,ValueError: Shape of passed values is (7, 4), indices imply (7, 2)

The codes below throw an exception, ValueError: Shape of passed values is (7, 4), indices imply (7, 2).

df4 = pd.DataFrame({'E': ['B2', 'B3', 'B6', 'B7'],
                     'F': ['D2', 'D3', 'D6', 'D7'],
                     'G': ['F2', 'F3', 'F6', 'F7']},
                    index=[2, 2, 6, 7])
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
                        'B': ['B0', 'B1', 'B2'],
                        'C': ['C0', 'C1', 'C2'],
                        'D': ['D0', 'D1', 'D2']},
                        index=[0, 1, 2])
result00 = pd.concat([df1, df4], axis=1,join='inner')

I am confused about the error. How to merge the two dataframe?

The result of merging i want is like below

enter image description here

Upvotes: 0

Views: 1771

Answers (2)

Devarshi Mandal
Devarshi Mandal

Reputation: 733

you can use the pd.concat in the following form:

result00 = pd.concat([df1, df4], axis=1, join_axes = [df4.index], join = 'inner').dropna()

The earlier code did not work since there was a duplicate index in df2. Hope this helps

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210822

you can use merge() method:

In [122]: pd.merge(df1, df4, left_index=True, right_index=True)
Out[122]:
    A   B   C   D   E   F   G
2  A2  B2  C2  D2  B2  D2  F2
2  A2  B2  C2  D2  B3  D3  F3

Upvotes: 3

Related Questions