首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:无法将输入数组从形状(5)广播到形状(7)

ValueError:无法将输入数组从形状(5)广播到形状(7)
EN

Stack Overflow用户
提问于 2018-09-19 08:13:37
回答 1查看 1.8K关注 0票数 1

在此代码中:

代码语言:javascript
复制
    import pandas as pd
    myj='{"columns":["tablename","alias_tablename","real_tablename","dbname","finalcost","columns","pri_col"],"index":[0,1],"data":[["b","b","vip_banners","openx","",["id","name","adlink","wap_link","ipad_link","iphone_link","android_link","pictitle","target","starttime","endtime","weight_limit","weight","introduct","isbutton","sex","tag","gomethod","showtype","version","warehouse","areaid","textpic","smallpicture","group","service_provider","channels","chstarttime","chendtime","tzstarttime","tzendtime","status","editetime","shownum","wap_version","ipad_version","iphone_version","android_version","showtime","template_id","app_name","acid","ab_test","ratio","ab_tset_type","acid_type","key_name","phone_models","androidpad_version","is_delete","ugep_group","author","content","rule_id","application_id","is_default","district","racing_id","public_field","editor","usp_expression","usp_group","usp_php_expression","is_pic_category","is_custom_finance","midwhitelist","is_freeshipping","resource_id","usp_property","always_display","pushtime","is_pmc","version_type","is_plan","loop_pic_frame_id","plan_personal_id","personal_id","is_img_auto","banner_type","ext_content"],"id"],["a","a","vip_adzoneassoc","openx","",["id","zone_id","ad_id","weight"],"id"]]}'
    df=pd.read_json(myj, orient='split')
    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']
    # 
    #bl=['add_time', 'allot_time', 'create_time', 'end_pay_time', 'start_pay_time', 'order_status,update_time', 'order_type,order_status,add_time', 'order_type,order_status,end_pay_time', 'wms_flag,order_status,is_first,order_date', 'last_update_time', 'order_code', 'order_date', 'order_sn', 'parent_sn', 'id', 'user_id', 'wms_flag,order_date']
    #al=['area_id', 'last_update_time', 'mobile', 'parent_sn', 'id', 'transport_number', 'parent_sn']

    def get_index(row):

        print(row)
        if row.tablename=='b':
            return bl
        else:
            return al
    #        return ['is_delete,status,author', 'endtime', 'banner_type', 'id', 'starttime', 'status,endtime', 'weight']
    df['index_cols']=df.apply(get_index,axis=1)

我遇到了错误:

ValueError:无法将输入数组从形状(5)广播到形状(7)

相反,如果我使用注释掉的blal,每件事都可以运行。如果我用

代码语言:javascript
复制
bl=['is_delete,status,author', 'endtime', 'banner_type', 'id', 'starttime', 'status,endtime']

它运行也很好,有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-20 08:51:49

pandas-0.22.0中,来自apply方法的列表可以用于构造新的数据帧,当它的长度等于初始数据from中的列数时。例如:

代码语言:javascript
复制
>>> df = pd.DataFrame([range(100),range(100)])

    0   1   2   3   4   5   6   7   8   9   ...     90  91  92  93  94  95  96  97  98  99
0   0   1   2   3   4   5   6   7   8   9   ...     90  91  92  93  94  95  96  97  98  99
1   0   1   2   3   4   5   6   7   8   9   ...     90  91  92  93  94  95  96  97  98  99

您可以在“应用程序”中返回列表并获取数据:

代码语言:javascript
复制
>>> df.apply(lambda x:(x+1).values.tolist(), axis=1)
    0   1   2   3   4   5   6   7   8   9   ...     90  91  92  93  94  95  96  97  98  99
0   1   2   3   4   5   6   7   8   9   10  ...     91  92  93  94  95  96  97  98  99  100
1   1   2   3   4   5   6   7   8   9   10  ...     91  92  93  94  95  96  97  98  99  100

但是,如果长度不匹配维数:

代码语言:javascript
复制
>>> df.apply(lambda x:(x+1).values.tolist()[:99], axis=1)

0    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...
1    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...

我们有系列赛。

如果返回不同维度的列表,而第一个维度与维度匹配,而下一个维度不匹配(就像在您的例子中那样),则会得到一个错误:

代码语言:javascript
复制
>>> df.apply(lambda x:[1] * 99 if x.name==0 else [0] * 100 , axis=1)
0    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
1    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...

工作正常。

而这个

代码语言:javascript
复制
>>> df.apply(lambda x:[1] * 100 if x.name==0 else [0] * 99 , axis=1)

引发错误。

pandas-0.23中,无论哪种方式,都可以获得系列赛:

代码语言:javascript
复制
>>> df.apply(lambda x:(x+1).values.tolist(), axis=1)
0    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...
1    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...

>>> df.apply(lambda x:(x+1).values.tolist()[:9], axis=1)
0    [1, 2, 3, 4, 5, 6, 7, 8, 9]
1    [1, 2, 3, 4, 5, 6, 7, 8, 9]

此问题不适用于pandas-0.22.0中的元组。

代码语言:javascript
复制
>>> df.apply(lambda x:(1,) * 9 if x.name==0 else (0,) * 10 , axis=1)
0       (1, 1, 1, 1, 1, 1, 1, 1, 1)
1    (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

你可以在你的案例中使用这个事实:

代码语言:javascript
复制
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')

>>> df.apply(get_index, axis=1)

0    (is_delete,status,author, endtime, banner_type...
1    (zone_id,ad_id, zone_id,ad_id,id, ad_id, id, z...
dtype: object
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52401172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档