Concatenating two data frames by column and index
Hi, so I have two dataframes, the first one is a dataframe that was created by grouping another df by id (which is now an index) and then sorting by the "due" column.
df1:
paid due
id
3 13.000000 5.000000
2 437.000000 5.000000
5 90.000000 5.000000
1 60.000000 5.000000
4 675.000000 5.000000
The other is a normal framework with three columns: 'id', 'name' and 'country'.
df2:
id name country
1 'AB' 'DE'
2 'CD' 'DE'
3 'EF' 'NL'
4 'HAH' 'SG'
5 'NOP' 'NOR'
So what I was trying to do was add the column "name" to the 1st dataframe based on the id number (which is the index in the first df and the column in the second). So I thought this code would work:
pd.merge(df1, df2['name'], left_index=True, right_on='id')
But I am getting error
ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
0
source to share
3 answers
You may find which pd.concat
is the best option here because it can accept a mix of data and series: http://pandas.pydata.org/pandas-docs/stable/merging.html#concatenating-with-mixed-ndims .
+1
source to share