gpt4 book ai didi

python - 值错误: could not broadcast input array from shape (5) into shape (7)

转载 作者:行者123 更新时间:2023-12-01 09:01:53 26 4
gpt4 key购买 nike

在此代码中:

    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: could not broadcast input array from shape (5) into shape (7)

相反,如果我使用注释掉的 blal 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

您可以在 apply 中返回一个列表并获取一个数据框:

>>> 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

关于python - 值错误: could not broadcast input array from shape (5) into shape (7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52401172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com