合并pandas中的TypeError当列中的项为列表时,列上的DataFrame

时间:2021-04-02 22:58:01

I got TypeError: type object argument after * must be an iterable, not itertools.imap for doing pd.merge 2 dataframes df1 and df_idenon the columns 'allmzidx' where data is list

我得到TypeError:类型对象参数在*必须是可迭代的,而不是itertools.imap用于执行pd.merge 2数据帧df1和df_idenon用于列'allmzidx',其中数据是列表

Simplified data:

# df1
df1 = pd.DataFrame({'a':[1110],'b':[1135],'c':[1160]})
df1['allmzidx'] = df1.values.tolist()
df1['allmzidx'] = df1.allmzidx.sort_values()

# df_iden is created from list of lists of tuples
alliden_tuple2 = [[('a2','b2',[1736, 1761, 1786]),('a12','b12',[1110, 1135, 1160])],[('a2','b2',[1736, 1761, 1786]  ),('a12','b12',[1110, 1135, 1160])]]
# for each list of tuples
for index, each_iden in enumerate(alliden_tuple2):
    df_iden = pd.DataFrame(each_iden, columns=['int','mztop3','allmzidx'])
    df_iden = pd.merge(df_iden, df1, how='left', on='allmzidx')

Why I can't merge the dataframes on the 'allmzidx' as list here?

为什么我不能将'allmzidx'上的数据帧合并为列表?

update Changing data to tuple works but I appreciate if someone may add the additional reasons why list doesn't work

更新将数据更改为元组可以正常工作但我很感激有人可能会添加列表不起作用的其他原因

1 个解决方案

#1


0  

I think need convert lists to tuples:

我认为需要将转换列表转换为元组:

df1['allmzidx'] = df1['allmzidx'].apply(tuple)

for index, each_iden in enumerate(alliden_tuple2):
    df_iden = pd.DataFrame(each_iden, columns=['int','mztop3','allmzidx'])
    df_iden['allmzidx'] = df_iden['allmzidx'].apply(tuple)
    df_iden = pd.merge(df_iden, df1, how='left', on='allmzidx')

#1


0  

I think need convert lists to tuples:

我认为需要将转换列表转换为元组:

df1['allmzidx'] = df1['allmzidx'].apply(tuple)

for index, each_iden in enumerate(alliden_tuple2):
    df_iden = pd.DataFrame(each_iden, columns=['int','mztop3','allmzidx'])
    df_iden['allmzidx'] = df_iden['allmzidx'].apply(tuple)
    df_iden = pd.merge(df_iden, df1, how='left', on='allmzidx')