import pandas as pd
df = pd.read_excel(r'C:\Users\wangkejun\Desktop\1.xls')
# 提取一一对应的数据
sku_list = []
channel_list = []
for sku, channel in zip(df['XXX'], df['XXXX']):
if pd.isna(channel): # 判断是否为缺失值
continue # 是缺失值则跳过该行数据
if ',' in str(sku): # 将sku转换为字符串类型
sku_items = str(sku).split(',')
channel_items = channel.split(',')
# 处理长度不一致的情况
min_len = min(len(sku_items), len(channel_items))
sku_list.extend(sku_items[:min_len])
channel_list.extend(channel_items[:min_len])
# 创建新的DataFrame
new_df = pd.DataFrame({
'XXX': sku_list,
'XXXX': channel_list
})
new_df.to_excel(r'C:\Users\wangkejun\Desktop\1.xlsx')
原数据?
结果?