我在Python3.6中编写了如下的嵌套函数-
full_df = unnest(full_df,'Options','product code')
def unnest(df, col, col2,reset_index=False):
for item in df[col]:
if len(item)==0:
item=item.append('')
col_flat = pd.DataFrame([[i, x]
for i, y in df[col].apply(list).iteritems()
for x in y ], columns=['I', col]
)
col_flat = col_flat.set_index('I')
df = df.drop(col, 1)
df = df.merge(col_flat, left_index=True, right_index=True)
if reset_index:
df = df.reset_index(drop=True)
if df[col] is None:
merchant_product_code = df['Product code']
else:
merchant_product_code = df['Product code'] + '-' + df[col]
df['item_group_id'] = df['Product code']
df['Product code'] = merchant_product_code
return df
在Options值为[]的情况下,我面临的问题是;删除[]为空,并在“产品代码”列中,在产品代码后添加连字符(-)。
我已将我的数据框full_df转换为字典,以便您对其进行测试。
{'Product code': {0: 'BBMTG', 1: 'BBDBPSD', 2: 'BBDBPEL', 3: 'BBDBPDR', 4: 'BBFTDR', 5: 'BBFTPBG', 6: 'BBFTPBS', 7: 'BBFTEY'}, 'Category': {0: 'Essentials', 1: 'Bedding /Bamboo Blanket', 2: 'Bedding /Bamboo Blanket', 3: 'Bedding /Bamboo Blanket', 4: 'Apparel', 5: 'Apparel', 6: 'Apparel', 7: 'Apparel'}, 'List price': {0: 8.9, 1: 45.0, 2: 45.0, 3: 45.0, 4: 28.0, 5: 28.0, 6: 28.0, 7: 28.0}, 'Price': {0: 8.9, 1: 45.0, 2: 45.0, 3: 45.0, 4: 28.0, 5: 28.0, 6: 28.0, 7: 28.0}, 'Options': {0: '[]', 1: '[]', 2: '[]', 3: '[]', 4: "['0-3m', '3-6m', '6-12m']", 5: "['0-3m', '3-6m', '6-12m']", 6: "['0-3m', '3-6m', '6-12m']", 7: "['0-3m', '3-6m', '6-12m']"}}
任何人都可以调查并帮助使其正常工作。
答案 0 :(得分:0)
最后,我可以通过更改以下行来解决我的问题, item.append('[]') 和 df ['产品代码'] + = df [col] .apply(lambda val:``if val ==“ []” else'-'+ val)