简体   繁体   English

我如何根据另一个具有不同长度但共享列数据的 dataframe 的条件向 dataframe 添加一列

[英]How can i add a column to a dataframe based on a conditional of another dataframe that has a different length, but shared column data

I have two dataframes of different lengths and different columns, but a shared column with the same identifying data.我有两个不同长度和不同列的数据框,但有一个具有相同标识数据的共享列。 They look like this他们看起来像这样

observations DF:观察DF:

index指数 scientific_name科学名称 park_name公园名称 observations观察
0 0 name1姓名1 park1公园1 10 10
1 1个 name2名字2 park2公园2 12 12

species DF:物种自由度:

index指数 scientific_name科学名称 common_names通用名称 category类别
0 0 name1姓名1 name1,name2名称 1,名称 2 Mammal哺乳动物
1 1个 name2名字2 name1,name2名称 1,名称 2 Vascular plant维管植物

I am trying to create a new column in the observatiosn DF called 'category' that is filled with data based on the shared scientific_names between both tables.我正在尝试在 observatiosn DF 中创建一个名为“category”的新列,该列基于两个表之间共享的 scientific_names 填充数据。 I've tried using pd.merge but it doesn't fill the category column the way I want.我试过使用 pd.merge 但它没有按照我想要的方式填充类别列。 Concat does not either. Concat 也没有。 When i tried using a list comprehension it gave me a value error too.当我尝试使用列表理解时,它也给了我一个值错误。 Any thoughts?有什么想法吗?

I tried using a list comprehension like so:我试过像这样使用列表理解:

observations['category'] = [el for el in species['category'] if observations['scientific_name'] == species['scientific_name]]

This results in an error.这会导致错误。

If you only wanted to add the "category" column from species to observations based on the shared column "scientific_name", this should work.如果您只想将species的“类别”列添加到基于共享列“scientific_name”的observations ,这应该可行。

observations = pd.merge(observations, species[['scientific_name', 'category']])

暂无
暂无

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

相关问题 如何将列添加到 dataframe 中,其值取决于另一个 dataframe? - How can I add a column to a dataframe with a value conditional on another dataframe? 如何将一列添加到基于另一个列值的数据框中? - How can I add a column to a dataframe that is based on another columns value? 如何将一列从一个数据帧添加到另一个数据帧? - How can I add a column from one dataframe to another dataframe? 如何根据DataFrame中元素的长度更改列值 - How can I change a column value based on length of element in a DataFrame 如何根据另一列中是否满足一组条件向 Python 中的数据框添加新列? - How can I add a new column to a dataframe in Python based on whether a set of conditions are met in another column? Pandas:根据另一个数据帧的长度添加列信息 - Pandas: Add column information based on length of another dataframe 如何基于具有不同行数的另一个 Dataframe 中的一个相似列删除一个 DataFrame 中的行 - How to drop rows in one DataFrame based on one similar column in another Dataframe that has a different number of rows 如何基于另一列将 append 数据转换为 dataframe? - How to append data to dataframe based on another column? 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何使用另一个数据框添加数据框并基于列更新公共值 - how to add a dataframe with another dataframe and updated common values based on a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM