coms
coms

Reputation: 22

pandas concat successful but error message following stops loop

I'm getting the error message

ValueError: No objects to concatenate

When using pd.concat, but the concat appears to be successful as when I try and print the resulting dataframe it does so successfully but the error message terminates the loop.

state_list = ['Colorado', 'Ilinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Michigan', 'Minnesota', 'Missouri', 'Nebraska',\
             'North Carolina', 'North Dakota', 'Ohio', 'Pennsylvania', 'South Dakota', 'Tennessee', 'Texas', 'Wisconsin']


    for state_name in state_list:
        ### DF1 is a dataframe unique to each state###
        condition_categories = (df1['description'].unique())
        cats = []
        for cat in condition_categories:
            category_df = df1[['week', 'value']].where(df1['description'] == cat).dropna()
            category_df = category_df.set_index('week')
            category_df = category_df.rename(columns={'value': str(cat)})
            category_df.week = dtype=np.int
            cats.append(category_df)
            #print(category_df)

        df = pd.concat(cats, axis =1) 
        print(df)

Upvotes: 0

Views: 1352

Answers (1)

Chandella07
Chandella07

Reputation: 2117

Sorry for the late response. Looks like there is some issue with "cats" list. It is empty in one of the iterations of outer for loop. You can add a condition just above concatination line. Like below, it may work.

if len(cats) > 0:
    df = pd.concat(cats, axis =1)
else:
    print("No records to concatinate")

Upvotes: 1

Related Questions