简体   繁体   中英

Print index of a list that contains a dictionary

I would like to print the second index of a list. In this particular case, it would be Col3. However, I am getting the error

---> 13 df_learn.index(2)

TypeError: 'RangeIndex' object is not callable

Here's what I am attempting.

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) 

My list actually has a dictionary inside, so how would I print a particular index it in this case?

When I do something like:

df_learn1 = df_learn[:1]
print(df_learn1)

I do get index 0 printed. Why would this work but not for a particular index? The following did not work either...

print(df_learn[2])

Thanks for your help!

Try this line it will work.

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

Or this

    df_learn['Col3']

You should try df_learn['col3'] the number that you want. This is because you acctaully have a list inside your dictionary not the other way around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM