• Register
0 votes
1.4k views

Problem :

I have the two DataFrames which I would want to merge. I have referred many documents and also tried to perform many operations but I am not sure what to do now. Please find my two  DataFrames as below:

DataFrame1:
     id      name  type   currency
0  BTTA.S   Apple  Hard      GBp
1  VOOD.S    Soft Soft      GBp

and

DataFrame2:
   id         price
BTTA.S     301.2215250
VOOD.S    213.7914000

and I would like to return:

      id    name    type    currency   price
0  BTTA.S   Apple  Hard      GBp  301.2215250
1  VOOD.S    Soft  Soft      GBp  213.7914000

Trying to merge the price column from the DataFrame2 with DataFrame1.

I have already tried many methods of doing this as below :

Result = DataFrame1.merge(DataFrame2[['*.S']], left_on='id', right_index=True) 

But here I met with the exception as below :

ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
6 5 3
7,540 points

1 Answer

0 votes

Solution :

 The error message clearly indicates that your DataFrame2 is of the type as pd.Series. You must try to convert your DataFrame2 .to_frame() as the .merge() needs the pd.DataFrame() as the input (please see docs to get clarity):
DataFrame1.merge(DataFrame2[['*.S']].to_frame(), left_on='id', right_index=True)

while you probably also just could do following :

DataFrame1.merge(DataFrame2.to_frame(), left_on='id', right_index=True)

Or you can use the pd.DataFrame.join() which accepts the pd.Series.

I hope this will help in resolving your issue.
9 7 4
38,600 points

Related questions

0 votes
1 answer 7 views
7 views
Problems I am trying to update selected datetime64 values in a pandas data frame using the loc method to select rows satisfying a condition. However, instead of assigning the new date-time value it results in NaT. Here is a simplification of my code that shows the problem: ... as the second element in the new_date column. Any ideas on how this should be done or why this is not working as intended?
asked Sep 15 Marivoke 530 points
0 votes
1 answer 3.1K views
3.1K views
Problem : I have the pandas data frame with some of the categorical predictors or variables as 0 & 1, and some of the numeric variables. When I fit that to a stasmodel like below : est = sm.OLS(y, X).fit() It throws the below error : Pandas data ... hundreds of variables. For that I have concatenated the 3 pandas DataFrames to come up with the final DataFrame to be used in the model building.
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 2.3K views
2.3K views
Problem : I have a big dataframe and I am trying to split that and after concat that but facing following error first argument must be an iterable of pandas objects, you passed an object of type "dataframe"
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 1.3K views
1.3K views
Problem : I am getting bellow error attributeerror: can only use .str accessor with string values, which use np.object_ dtype in pandas
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 193 views
193 views
Problem: I have only fundamental knowledge related to python, pandas and dataframe.I have tried to write the below code: df = pd.DataFrame(np.random.rand(12,2), columns=['Apples', 'Oranges'] ) df['Categories'] = pd.Series(list('AAAABBBBCCCC')) pd.options.display. ... I will be more than glad to get more ideas on the above error regarding why it is occurring and also how to fix this error?
asked Aug 24 Raphael Pacheco 4.9k points