my starting point looks like this
import pandas as pd
dx = {
'bezeichnung': ['Erlöse' , 'Aufwand' , 'Material_19' , 'Gewinn'] ,
'2020': ['1001' , '900' , '90' , '11']
}
dx = {
'bezeichnung': ['Aufwand' , 'Erlöse' , 'Material_16' , 'Gewinn'] ,
'2019': ['1900' , '2001' , '80' , '21']
}
df1 = pd.DataFrame(dx)
df2 = pd.DataFrame(dy)
I want basically the following:
- Compare the columns named 'bezeichnung'. If the elements in both columns are equal to another, add the respective value of '2019' in a new column '2019' which should be either added to df1 or a new df3.
- If an element of 'bezeichnung' in df2 is not found in df1 add the element at the end of column 'bezeichnung' in df1 and put the corresponding value in df2 '2019' to the added column '2019' (see above).
- It is important that the order within column 'bezeichnung' in df1 is maintained.
The result should look like this:
df1 = pd.DataFrame('bezeichnung': ['Erlöse' , 'Aufwand' , 'Material_19' , 'Gewinn', 'Material_16'] ,
'2020': ['1001' , '900' , '90' , '11', '0'] ,
'2019': ['2001' , '1900' , '0' , '21', '80'])
Thank you very much!