Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Index of dataframes in a list

I have a list of dataframes and want to use their indexes for my operations.

import pandas as pd
import numpy as np

I0 = pd.DataFrame({'Eng': [11], 
                  'Fr': [10], 
                  'It': [9],
                  'Sp': [11],
                  'Jp': [12]
                  })
I1 = pd.DataFrame({'Eng': [9], 
                  'Fr': [4], 
                  'It': [9],
                  'Sp': [11],
                  'Jp': [15]
                  })
I = [I0, I1]

When I do I.index(I0) it gives me 0 which is correct but I.index(I0) gives me the following error:

Screenshot of the error when asking for another index

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any other function or method to have the indexes of the dataframes.

Thank you in advance

  1. I switched the dataframes and got the same error
  2. I used tuples and sets instead of lists and got the same error
  3. I switched the order of the dataframes in the list and got the same error

>Solution :

Solution:

One of the probably many ways to do this would be to make a dictionary of the dataframes rather than a list. For example

import pandas as pd
import numpy as np

I0 = pd.DataFrame({'Eng': [11], 
                  'Fr': [10], 
                  'It': [9],
                  'Sp': [11],
                  'Jp': [12]
                  })

I1 = pd.DataFrame({'Eng': [9], 
                  'Fr': [4], 
                  'It': [9],
                  'Sp': [11],
                  'Jp': [15]
                  })

I = {'I0':I0, 'I1':I1}

list(I.keys()).index("I1")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading