打印包含字典的列表的索引

时间:2020-04-29 11:58:42

标签: python list dictionary printing python-3.7

我想打印列表的第二个索引。在这种情况下,它将是Col3。但是,我得到了错误

---> 13 df_learn.index(2)

TypeError:“ RangeIndex”对象不可调用

这就是我想要的。

l = 'Date of birth'
fls = browser.find_elements_by_xpath('//label[contains(.,"{}")]'.format(l))
    if fls:
        # loop through array <<ar>> and pass each value one by one  
        element = browser.find_elements_by_xpath('//*[@id="{}"]'.format(fls[0].get_attribute("for")))

        if element:
            containtInput = element[0].tag_name.lower() == "input"
            if containtInput:
                x = element[0].find_elements_by_xpath(./following::/input[0])
                x.send_keys(value)
                break

我的列表中实际上有一个字典,那么在这种情况下如何打印特定的索引?

当我做类似的事情时:

import pandas as pd

df_learn = pd.DataFrame({'Col1': [10, 20, 15, 30, 45],
                   'Col2': [13, 23, 18, 33, 48],
                   'Col3': [17, 27, 22, 37, 52],
                   'Col4': [34, 7, 12, 44, 21]})

df_learn.index(2) 

我确实打印了索引0。为什么这项工作有效,但不适用于特定索引? 以下内容也不起作用...

df_learn1 = df_learn[:1]
print(df_learn1)

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

尝试此行即可。

    df_learn.iloc[:, 2]#to get column values of 2nd index
    #df_learn.iloc[2] #to get 2nd element of each column

或者这个

    df_learn['Col3']

答案 1 :(得分:1)

您应该尝试echo abc,wer | tr , '\n' 您想要的号码。这是因为您实际上在字典中有一个列表,而不是相反。