简体   繁体   English

如何根据行中的另一个值在 dataframe 中创建列(Python)

[英]How to create a column in a dataframe based on another value in the row (Python)

I have the following data:我有以下数据:

country国家 code代码 continent大陆 plants植物 invertebrates无脊椎动物 vertebrates脊椎动物 total全部的
Afghanistan阿富汗 AFG AFG Asia亚洲 5 5 2 2 33 33 40 40
Albania阿尔巴尼亚 ALB ALB Europe欧洲 5 5 71 71 61 61 137 137
Algeria阿尔及利亚 DZA DZA Africa非洲 24 24 40 40 81 81 145 145

I want to add a hemisphere columns that is determined on by the continent that references a list.我想添加一个由引用列表的大陆确定的半球列。 I want to do it using a custom function (and not using lambda).我想使用自定义 function (而不是使用 lambda)来做到这一点。

I attempted the following:我尝试了以下操作:

northern = ['North America', 'Asia', 'Europe']
southern = ['Africa','South America', 'Oceania']

def hem(x,y):
    if y in northern:
        x = 'northern'
        return x
       
    elif y in southern:
        x = 'southern'
        return x
           
    else:
        x = 'Not Found'
        return x

species_custom['hemisphere'] = species_custom.apply(hem, args=(species_custom['continent'],), axis=1)

I receive the following error:我收到以下错误:

ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')

Any ideas where I am going wrong?有什么想法我哪里出错了吗?

hem is defined as taking two arguments but in the apply you only pass one. hem被定义为服用两个 arguments 但在apply中你只通过一个。 And when you do you are passing the full continent column to it.当你这样做时,你正在将整个continent列传递给它。 Probably not what you want.可能不是你想要的。

You could simplify by using nested numpy where .您可以通过使用嵌套numpy来简化where .

import numpy as np

df['hemisphere'] = np.where(df['continent'].isin(northern), 'northern', np.where(df['continent'].isin(southern),'southern','Not Found'))

Result结果

       country code continent  plants  invertebrates  vertebrates  total  hemisphere
0  Afghanistan  AFG      Asia       5              2           33     40    northern 
1      Albania  ALB    Europe       5             71           61    137    northern 
2      Algeria  DZA    Africa      24             40           81    145    southern 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个 pandas 系列(列),基于与另一个 Dataframe 中的值的匹配? - How to create a pandas Series (column), based in a match with a value in another Dataframe? Python数据框:基于另一列创建列 - Python Dataframe: Create columns based on another column 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 基于另一个 dataframe 的行值对一个 dataframe 中的列求和 - Sum column in one dataframe based on row value of another dataframe 如何根据 Row_id 列将值写入 dataframe 的另一列并且匹配列中存在值? - How to write the values to another column of dataframe based on Row_id column and value exist in match column? Label 基于另一列(同一行)的值的列 pandas dataframe - Label a column based on the value of another column (same row) in pandas dataframe Python-根据条件将一列添加到包含来自另一行的值的数据框 - Python - Add a column to a dataframe containing a value from another row based on condition 如何根据 Pandas dataframe 中上一行的行值创建新列? - How to create a new column based on row value in previous row in Pandas dataframe? 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM