Search code examples
pythonpandasdataframenumpydata-analysis

Is there a way to iterate through a column in pandas if it is an index


I have a pandas DataFrame which looks like this

Region    Sub Region          Country     Size      Plants  Birds   Mammals
Africa    Northern Africa     Algeria     2380000   22      41      15
                              Egypt       1000000   8       58      14
                              Libya       1760000   7       32      8
          Sub-Saharan Africa  Angola      1250000   34      53      32
                              Benin       115000    20      40      12
          Western Africa      Cape Verde  4030      51      35      7
Americas  Latin America       Antigua     440       4       31      3
                              Argentina   2780000   70      42      52
                              Bolivia     1100000   106     8       55
          Northern America    Canada      9980000   18      44      24
                              Grenada     340       3       29      2
                              USA         9830000   510     251     91
Asia      Central Asia        Kazakhstan  2720000   14      14      27
                              Kyrgyz      200000    13      3       15
                              Uzbekistan  447000    16      7       19
          Eastern Asia        China       9560000   593     136     96
                              Japan       378000    50      77      49
                              South Korea 100000    31      28      33

So I am trying to prompt the user to input a value and if the input exists within the Sub Region column, perform a particular task.

I tried turning the 'Sub region' column to a list and iterate through it if it matches the user input

    sub_region_list=[]
    for i in world_data.index.values:
        sub_region_list.append(i[1])
        print(sub_region_list[0])

That is not the output I had in mind. I believe there is an easier way to do this but can not seem to figure it out


Solution

  • You can use get_level_values to filter.

    sub_region = input("Enter a sub region:")
    if sub_region not in df.index.get_level_values('Sub Region'):
        raise ValueError("You must enter a valid sub-region")
    

    If you want to save the column values in a list, try:

    df.index.get_level_values("Sub Region").unique().to_list()