[英]How To Fill New Column With Values From Second Dataframe but Dependent on Different Existing Column in Current Dataframe using Pandas
I created an informational table, which is very long (think nearly 100 observations or just under).我创建了一个信息表,它很长(想想近 100 次观察或略低于)。 I have a main table (around 70K onservations) where I need to create a new column and fill it based on matching values between my two dataframes but I need to fill the new column the cells with data from my information table.我有一个主表(大约 70K onservations),我需要在其中创建一个新列并根据我的两个数据帧之间的匹配值填充它,但我需要用我的信息表中的数据填充新列的单元格。
I created a small dataset but my real dataset (which I can't share because my prof signed a non-diclosure) has like 70K observations.我创建了一个小数据集,但我的真实数据集(我无法分享,因为我的教授签署了保密协议)大约有 70K 观察。
data_1 (info table) data_1(信息表)
Animal Food
Dog Stake
Cat Fish
Rabbit Carrot
data 2 (original table)数据2(原表)
Name Animal Age
Binxy Dog 1
Al Rabbit 4
Sam Dog 11
Dexter Cat 9
Dory Hamster 6
Chloe Cat 5
desired data_frame所需的数据帧
Name Animal Age Fed [new column]
Binxy Dog 1 Stake
Al Rabbit 4 Carrot
Sam Dog 11 Stake
Dexter Cat 9 Fish
Dory Hamster 6 NaN
Chloe Cat 5 Fish
my gut tells me it probably has to do with iloc
or loc
and using some bloonean values.我的直觉告诉我这可能与iloc
或loc
以及使用一些 bloonean 值有关。 Verbally I would say:口头上我会说:
I think merge
might work but I'm not certain if it would fill it for every matching value.我认为merge
可能会起作用,但我不确定它是否会为每个匹配的值填充它。 I'm not very good at merges because I struggle to understand the join feature but I don't think values would be inserted where I need them because my dataframes are not the same length.我不太擅长合并,因为我很难理解连接功能,但我认为不会在我需要它们的地方插入值,因为我的数据帧长度不同。
edit: I've don't manually before but I had like only two or three values in fill in and I don't want to manually do this for 100. But this is as far as my basic understanding goes.编辑:我以前没有手动操作过,但我只需要填写两三个值,我不想手动为 100 执行此操作。但这是我的基本理解。
New_Categorized_Full.loc[
(New_Categorized["Produce"] == "Apple"), "Fruit"] = "Fuji"
Use series.map
使用series.map
df2['Fed'] = df2.Animal.map(dict(df1[['Animal','Food']].to_numpy()))
Out[10]:
Name Animal Age Fed
0 Binxy Dog 1 Stake
1 Al Rabbit 4 Carrot
2 Sam Dog 11 Stake
3 Dexter Cat 9 Fish
4 Dory Hamster 6 NaN
5 Chloe Cat 5 Fish
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.