简体   繁体   English

遍历两个熊猫数据帧,并将数据从一个数据帧附加到另一个

[英]Iterating through two pandas dataframes and appending data from one dataframe to the other

I have two pandas data-frames that look like this: 我有两个看起来像这样的熊猫数据框:

data_frame_1: data_frame_1:

index      un_id   city
1          abc     new york
2          def     atlanta
3          gei     toronto
4          lmn     tampa

data_frame_2: data_frame_2:

index   name     un_id
1       frank    gei
2       john     lmn
3       lisa     abc
4       jessica  def

I need to match names to cities via the un_id column either in a new data-frame or an existing data-frame. 我需要通过新数据框架或现有数据框架中的un_id列将名称与城市匹配。 I am having trouble figuring out how to iterate through one column, grab the un_id, iterate through the other un_id column in the other data-frame with that un_id, and then append the information needed back to the original data-frame. 我在弄清楚如何迭代一列,获取un_id,使用该un_id遍历另一个数据帧中的另一个un_id列时遇到麻烦,然后将所需的信息附加回原始数据帧。

use pandas merge : 使用熊猫合并

In[14]:df2.merge(df1,on='un_id')
Out[14]: 
      name un_id      city
0    frank   gei   toronto
1     john   lmn     tampa
2     lisa   abc  new york
3  jessica   def   atlanta

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

相关问题 根据特定条件将来自两个单独的 pandas 数据帧的行附加到一个 dataframe - Appending rows from two separate pandas dataframes onto one dataframe based on certain conditions Select 数据从两个数据帧转换成两个数据帧 Pandas DataFrame 和 ZA7F5F35426B567411FCZ9231 - Select data from two dataframes into two dataframes with Pandas DataFrame and Python 使用两列连接,在四个其他数据帧中填充一个pandas数据帧中的列 - using two column join, populate columns in one pandas dataframe from four other dataframes Pandas 使用来自其他 2 个查找数据帧的值在一个数据帧中提取数据 - Pandas extracting data in one Dataframe using values from 2 other lookup Dataframes 比较熊猫数据帧,从其他数据帧捕获数据 - Compare pandas dataframes, capture data from other dataframe 创建标准 dataframe 以接收来自 Pandas 中其他数据帧的数据 - Create a standard dataframe to receive data from other dataframes in Pandas 迭代熊猫数据框中的两列 - Iterating through two columns in a pandas dataframe Pandas Dataframe一次迭代两行 - Pandas Dataframe iterating through two rows at a time Pandas 从 n 个数据帧中提取数据并将其合并到一个数据帧中 - Pandas pickup and merge data from n-dataframes into one dataframe 遍历 JSON 并附加到 dataframe - Iterating through JSON and appending into dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM