简体   繁体   English

如何使用 Pandas 使用来自第二个数据帧但依赖于当前数据帧中不同现有列的值填充新列

[英]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.我的直觉告诉我这可能与ilocloc以及使用一些 bloonean 值有关。 Verbally I would say:口头上我会说:

  1. If data_2["Animal"] == data_2["Animal"]如果 data_2["Animal"] == data_2["Animal"]
  2. Then fill new column data_2["Fed"] with corresponding food found in data_1["Food"]然后用 data_1["Food"] 中找到的相应食物填充新列 data_2["Fed"]

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.

相关问题 Python Pandas:如何在DataFrame列中现有值之间填充值? - Python pandas: how to fill values between existing ones in dataframe column? 如果 dataframe 2 的 A 列与 pandas 的 A 列不同,如何将 dataframe 1 的 A 列中的值归零? - How to zero values in column A of dataframe 1 if they are different from column A of dataframe 2 in pandas? 如何从 pandas dataframe 中的现有列创建新列 - How to create a new column from an existing column in a pandas dataframe 通过将其他数据框的列值和标量传递给Pandas Python中的函数,在第二个数据框中创建新列? - Create a new column in a second dataframe by passing column values of a different dataframe and a scalar to a function in Pandas Python? Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 如何使用现有列名称和值在Pandas数据框中创建列表的新列? - How to create new column of lists in Pandas dataframe using existing column names and values? Pandas:更新第二个数据帧的列值 - Pandas: update column values from second dataframe 如何将新列添加到现有 pandas dataframe - How to add a new column to an existing pandas dataframe 如何在现有 pandas dataframe 中填充新列 - How to populate a new column in an existing pandas dataframe 如何将新列分配给 Pandas 中的现有 DataFrame - How to assign new column to existing DataFrame in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM