Resolving Dimension Mismatch in Function Output with Pandas DataFrame

The issue you’re facing is due to the mismatch in dimensions between bl and al. When the function returns a tuple of different lengths, it gets converted into a Series. To fix this, you can modify your function to return both lists at the same time:

def get_index(x):
    bl = ('is_delete,status,author', 'endtime', 'banner_type',
          'id', 'starttime', 'status,endtime', 'weight')
    al = ('zone_id,ad_id', 'zone_id,ad_id,id', 'ad_id', 'id', 'zone_id')

    if x.name == 0:
        return (list(b) + list(a)[:len(b)]) 
    else:
        return (list(b) + list(a)[9:]) 
    
df.apply(get_index, axis=1)

This way, it will always be a Series.


Last modified on 2023-06-26