Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 =

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

|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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading