NotAName
NotAName

Reputation: 4322

Pandas merge not working due to a wrong type

I'm trying to merge two dataframes using

grouped_data = pd.merge(grouped_data, df['Pattern'].str[7:11]
                      ,how='left',left_on='Calc_DRILLING_Holes',
                      right_on='Calc_DRILLING_Holes')

But I get an error saying can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

What could be the issue here. The original dataframe that I'm trying to merge to was created from a much larger dataset with the following code:

import pandas as pd
raw_data = pd.read_csv(r"C:\Users\cherp2\Desktop\test.csv")

data_drill = raw_data.query('Activity =="DRILL"')

grouped_data = data_drill.groupby([data_drill[
                                  'PeriodStartDate'].str[:10], 'Blast'])[
                                  'Calc_DRILLING_Holes'].sum().reset_index(
                                  ).sort_values('PeriodStartDate')

What do I need to change here to make it a regular normal dataframe?

If I try to convert either of them to a dataframe using .to_frame() I get an error saying that 'DataFrame' object has no attribute 'to_frame'

I'm so confused at to what kind of data type it is.

Upvotes: 0

Views: 221

Answers (1)

John Ladasky
John Ladasky

Reputation: 1064

Both objects in a call to pd.merge need to be DataFrame objects. Is grouped_data a Series? If so, try promoting it to a DataFrame by passing pd.DataFrame(grouped_data) instead of just grouped_data.

Upvotes: 2

Related Questions