简体   繁体   中英

pd.concat in pandas is giving a TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

I have 2 dataframes. I am slicing them to create 2 sub set dataframes. After subsets are created, I want to concat those 2 subsets and then analyse the concatenated subset.

1st subset dataframe

year_d_df = df_calender[['year']]

2nd subset dataframe

daily_df_train = pd.DataFrame(df_train.loc[:,'d_1':].sum(), columns = ['Count'])

Renaming 2nd subset dataframe indexes to make them same as sub set dataframe 1. This is a precondition to concatenation. Indexes must be same otherwise NaN values are inserted as part of concatenation process.

idx=0
for value in daily_df_train.index:
    if idx < 1914:
        daily_df_train.rename({value: idx}, axis = 'index', inplace=True)
        idx += 1

Above 2 subsets are created. If I do a type on them, they return as dataframe. But when I try to concatenate them, I get the TypeError.

Concatenating subset dataframes:

yearly_trend = pd.concat(['daily_df_train','years_d_df'])

TypeError                                 Traceback (most recent call last)
<ipython-input-101-e5e68495a055> in <module>
        yearly_trend = pd.concat(['daily_df_train','years_d_df'])

----Error
~\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

Both subsets are dataframe. They both have 1 column each which is a int64 type. Then, why is this code generating the TypeError?

I think the problem is that the variable identifiers are in quotes. Thus, you try to concatenate strings instead of the dataframes themselves. Try:

    yearly_trend = pd.concat([daily_df_train,years_d_df])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM