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
Is there any other function or method to have the indexes of the dataframes.
Thank you in advance
- I switched the dataframes and got the same error
- I used tuples and sets instead of lists and got the same error
- 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")