在此代码中:
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)
相反,如果我使用已注释掉的bl
和al
,则一切正常。
另外,如果我使用
bl=['is_delete,status,author', 'endtime', 'banner_type', 'id', 'starttime', 'status,endtime']
它也运行正常,这是什么问题?
答案 0 :(得分:1)
在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