简体   繁体   中英

cannot concatenate object of type "<class 'float'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

I have here df2, which is a range of values from 1st Jan to 26th Aug. I created a new data frame defining there column, spring summer and Autumn. I want to move values within each range of dates for each season into the appropriate columns in the new dataframe. I got this error and am unsure where to go next. Anyone have any ideas?

'print(df2)
seasons = (pd.DataFrame(columns = ['Winter','Spring','Summer', 'Autumn']))
Sp = df2[pd.date_range(start = '01/01/2020', end = '28/02/2020')]
for value in Sp:
    if value >0:
        seasons['Winter'].append(value)'

In general, when building DataFrames in pandas, you're better off collecting your data in native collections (lists, dicts, tuples), and then turning them into a DataFrame at the end. Adding cells or rows to a DataFrame bit by bit is slow.

The specific error you're getting here is that append only works for combining pandas objects.

You can use .loc assignment to do what you're describing, but it's not recommended:

for i, value in enumerate(Sp.values):
    seasons.loc[i, 'Winter'] = value

But the way you are pulling values makes me think a new table doesn't make the most sense here.

It might be better to add a new column to df2 called Season that labels each row as Winter, Summer, etc based on date. Then you can use groupby('Season') or query('Season == "Winter"') to pull out the data season by season.

To create the Season column, you just need a function that will tell you what season a row is in. I don't know what's in your table, and I'm not that good with datetime objects, but it would be in this form:

def assign_season(index):
    season = ...
    return season

Then you create the column with something like:

df2['Season'] = [assign_season(x) for x in df.index]

You don't have to use the index, you can use a column, but the form is basically the same. Just make sure the function is designed for what you're passing to it. You can also key off multiple column values:

def assign_season(val1, val2):
    ...

df2['Season'] = [assign_season(*vals) 
                 for vals in df2[['Column1', 'Column2']].values] 

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