[英]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 ![]() |
name1![]() |
park1![]() |
10 ![]() |
1 ![]() |
name2![]() |
park2![]() |
12 ![]() |
species DF:物种自由度:
index![]() |
scientific_name![]() |
common_names![]() |
category![]() |
---|---|---|---|
0 ![]() |
name1![]() |
name1,name2![]() |
Mammal![]() |
1 ![]() |
name2![]() |
name1,name2![]() |
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.