Error while merging data in Pandas

Asked 2 years ago, Updated 2 years ago, 38 views

Why do I get an error when I run the following program with the date key and merge_df1 as a left external connection?

import numpy as np
import pandas aspd

series=pd.Series('1/1', '1/2', '1/3', '1/4', '1/5', '1/6', '1/7',
                '1/8', '1/9', '1/10', '1/11', '1/12', '1/13', '1/14', name = 'date')
df1 = pd.DataFrame({'Date':['1/1', '1/2', '1/3', '1/4', '1/6', '1/7', ],
                    "Weather": ["Clear", "Cloudy", "Cloudy", "Cloudy", "Cloudy", "Cloudy"]})
df2 = pd.DataFrame({ 'Date': ['1/8', '1/14', '1/9', '1/12', '1/11',] ,
                    Weather: ["rain", "clear", "clear", "rain", "clear"]})

# Connecting the two DataFrames vertically
merge_df1 = np.concatenate([df1,df2],axis=0)  
print(f'Connected:\n{merge_df1}\n')

# Left external merge_df1 with date key to series
merge_df2 = pd.merge(series, merge_df1, on = 'date', how = 'left')
print(f'left external connection with date key:\n{merge_df2}\n')

python pandas

2022-09-30 17:05

1 Answers

Let's start with the error statement.

I moved it myself and took it out.

Can only merge Series or DataFrame objects, a<class 'numpy.ndarray'>was passed

Only series or data frame objects can be merged, but a numpy array has been passed.
It means

In other words, either series or merge_df1 can be a numpy array object.
Let's print the mold.

print(type(series))
print(type(merge_df1))

<class'pandas.core.frame.DataFrame'>
<class'numpy.ndarray'>

merge_df1 is not a data frame but a numpy array.
I also noticed here that it is bound by np.concatenate(), so it's obvious.

I would appreciate it if you could check it like this.
Let's check again with "pandas dataframe combination" etc.


2022-09-30 17:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.