在熊猫中发布迭代数据框

时间:2019-10-17 00:21:12

标签: python pandas dataframe

我正在使用以下列表的内容填充数据框:

desc_prep=[['aesthet', 'abod'], [['arb', 'abod'], ['forest', 'abod']]]

col_names =  ['desc_name','desc_avg_vector']
df_desc_prep = pd.DataFrame(columns=col_names)    
df_desc_prep['desc_name']=desc_prep

此时,我得到以下信息:

                         desc_name
0                [aesthet, abod]
1  [[arb, abod], [forest, abod]]

当使用迭代项迭代数据框以获取具有列名称和内容为系列的元组时:

for index, value in df_desc_prep.iteritems():
    print("index: ", index)#-->index:  desc_name
    print("value: ", value)#-->value:  0                  [aesthet, abod]
    print("value[0]:", value[0])#['aesthet', 'abod']
    print("value[1]:", value[1])#[['arb', 'abod'], ['forest', 'abod']]
    if isinstance(value[0], list):#->value[0]:  ['aesthet', 'abod']

使用iterrows()进行迭代以获取每一行的序列时:

for index, value in df_desc_prep.iterrows():
    print("index: ", index)#-->index:  0
    print("value: ", value)#-->value:  desc_name    [aesthet, abod]
    if isinstance(value[0], list):#-->value[0]:  ['aesthet', 'abod'], value[1]: IndexError: index out of bounds

我期望将值[0]设为aesthet,将值[1]设为abod。相反,我在获得价值[1]时得到IndexError: index out of bounds

如何获得在数据帧上进行迭代并在value[0]=aesthet上进行迭代时获得['aesthet', 'abod']和在value[0]=['arb', 'abod']上进行迭代时得到[['arb', 'abod'], ['forest', 'abod']]的行为

1 个答案:

答案 0 :(得分:1)

...
for index, value in df_desc_prep.iterrows():
    print(value[0][0])
    print(value[0][1])

aesthet
abod
['arb', 'abod']
['forest', 'abod']

for index, value in df_desc_prep.iterrows():
    print(value['desc_name'][0])
    print(value['desc_name'][1])

aesthet
abod
['arb', 'abod']
['forest', 'abod']