[英]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我有一个 pandas DataFrame 看起来像这样
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.所以我试图提示用户输入一个值,如果输入存在于 Sub Region 列中,则执行特定任务。
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.那不是我想到的 output。 I believe there is an easier way to do this but can not seem to figure it out我相信有一种更简单的方法可以做到这一点,但似乎无法弄清楚
You can use get_level_values
to filter.您可以使用get_level_values
进行过滤。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.