【问题标题】:Print index of a list that contains a dictionary打印包含字典的列表的索引
【发布时间】:2020-04-29 11:58:42
【问题描述】:

我想打印列表的第二个索引。在这种特殊情况下,它将是 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])

感谢您的帮助!

【问题讨论】:

    标签: python list dictionary printing python-3.7


    【解决方案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']
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 2018-09-13
        • 1970-01-01
        相关资源
        最近更新 更多