簡體   English   中英

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

[英]Print index of a list that contains a dictionary

我想打印列表的第二個索引。 在這種特殊情況下,它將是 Col3。 但是,我收到了錯誤

---> 13 df_learn.index(2)

類型錯誤:'RangeIndex' object 不可調用

這是我正在嘗試的。

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']您想要的數字。 這是因為您在字典中確實有一個列表,而不是相反。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM