Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法将输入数组从形状(5)广播到形状(7)_Python_Python 3.x_Pandas - Fatal编程技术网

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

Python ValueError:无法将输入数组从形状(5)广播到形状(7),python,python-3.x,pandas,Python,Python 3.x,Pandas,在此代码中: 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_

在此代码中:

    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)广播到形状 (七)

相反,如果我使用注释掉的
bl
al
evevything运行良好。 如果我使用

bl=['is_delete,status,author', 'endtime', 'banner_type', 'id', 'starttime', 'status,endtime']

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

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

>>> 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
您可以在“应用”中返回列表并获取数据帧:

>>> 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
但如果长度与尺寸不匹配:

>>> 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...
我们有一个系列

如果返回不同维度的列表,且第一个维度与该维度匹配,而下一个维度与之不匹配(与您的情况类似),则会出现错误:

>>> 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, ...
工作正常

这个呢

>>> df.apply(lambda x:[1] * 100 if x.name==0 else [0] * 99 , axis=1)
引发一个错误

pandas-0.23
中,您可以通过任意一种方式获得一个系列:

>>> 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
中的元组:

>>> 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)
您可以在您的案例中使用此事实:

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

你的熊猫版本是什么?我的版本是pd:“0.22.0”“0.20.3”不起作用。你能更新帖子以便清楚哪些部分不起作用,哪些部分应该起作用吗?bl=['is_delete,status,author','endtime','banner\u type','id','starttime','status,endtime','weight']al=['zone_id,ad_id','zone_id,ad_id,id','ad_id','id','zone_id']工作:bl=['is_delete,status,author','endtime','banner_type','id','starttime','status,endtime']