Pandas Adding column in dataframe from from another dataframe causing error

I am having a dataframe df1 and df2

df1=

|carno | sales |
|------|------ |
|101   |   1   |
|102   |   2   |
|103   |   6   |
|101   |   8   |

df2 =

|jeepno| jsales|
|------|-------|
|107   |    3  |
|108   |    9  |

I want to add df2 columns in df1
I actually want the df1 column to be like this
df1 =

carno sales jeepno jsales
101 1 107 3
102 2 108 9
103 6 nan nan
101 8 nan nan

I tried below methed
df1['jeepno'] = nan df1['jsales'] = nan
I want to try using = method
df1['jeepno'] = df2['jeepno'] df1['jsales'] = df2['jsales']
the above method is working in python 3.10 but in my friends 3.6 its failing with error
"valueerror cannot reindex from a duplicate axis"

Pls assist

>Solution :

No need to manually set columns with nan, just concat dataframes columnwise with

pd.concat([df1, df2], axis=1)

   carno  sales  jeepno   jsales
0    101      1    107.0     3.0
1    102      2    108.0     9.0
2    103      6      NaN     NaN
3    101      8      NaN     NaN

Leave a Reply