你的代码存在缩进错误。请将if
语句后面的代码块正确缩进,如下所示:
def big_countries(world: pd.DataFrame) -> pd.DataFrame:
A = np.all(world.iloc[:,2:4] >= [300000, 2500000])
if A:
print(world.loc[:,['name','area','population']])
在这个例子中,if
语句的代码块应该相对于前一行有一个额外的缩进。
如果你希望将结果返回为DataFrame而不是直接打印出来,请修改代码如下:
def big_countries(world: pd.DataFrame) -> pd.DataFrame:
A = np.all(world.iloc[:,2:4] >= [300000, 2500000])
if A:
return world.loc[:,['name','area','population']]
这样修改后,函数会返回符合条件的DataFrame。你可以在函数调用时接收返回值并进行其他操作或打印输出。
内容由零声教学AI助手提供,问题来源于学员提问