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

Print index of a list that contains a dictionary

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

---> 13 df_learn.index(2)

TypeError: 'RangeIndex' 对象不可调用

这是我正在尝试的。

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) 

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

当我做类似的事情时:

df_learn1 = df_learn[:1]
print(df_learn1)

我确实打印了索引 0。为什么这会起作用但不适用于特定索引? 以下也不起作用...

print(df_learn[2])

感谢您的帮助!

试试这行它会起作用。

    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']

您应该尝试 df_learn['col3'] 您想要的号码。这是因为你的字典中确实有一个列表,而不是相反。