• Register
0 votes
2.3k 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

Please log in or register to answer this question.

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 27 views
27 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, 2020 Marivoke 530 points
0 votes
1 answer 4.4K views
4.4K 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.9K views
2.9K 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.8K views
1.8K 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 15 views
15 views
Problem: I have the following indexed DataFrame with named columns and strings that are not contiguous numbers: a b c d 2 0.671399 0.101208 -0.181532 0.241273 3 0.446172 -0.243316 0.051767 1.577318 5 0.614758 0.075793 -0.451460 -0.012493 I would like to ... different versions join, append, mergebut I did not get the desired result, most only errors. How do I add a column eto the above example?
asked Dec 23, 2020 sasha 5.3k points
0 votes
1 answer 27 views
27 views asked Oct 30, 2020 john 580 points
0 votes
1 answer 112 views
112 views
Here's what I've got. I have two data frames. One is a set of financial data that already exists in the system and another set that has some that may or may not exist in the system. I need to find the difference and add the ... pandas\core\frame.py", line 3571, in _compare_frame raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame
asked Oct 28, 2020 psandprop 2.4k points
0 votes
1 answer 1.1K views
1.1K 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, 2020 Raphael Pacheco 4.9k points
0 votes
0 answers 16 views
16 views
Problem: I am new to pandas pivot table function I am following this tutorial [1]. I am looking at an existing Excel file and trying to generate a table output. I am trying to index a pivot table like; def generate_invoice_summary_info(): file_path = 'output.xlsx ... types to aggregate Why am I getting this error? I am trying to sum my table based on Cost Center Accounts, Account Category columns
asked Jan 1 sasha 5.3k points