简体   繁体   English

使用两列连接,在四个其他数据帧中填充一个pandas数据帧中的列

[英]using two column join, populate columns in one pandas dataframe from four other dataframes

The end result Pandas Data Frame needs to look something like this. 最终结果Pandas Data Frame需要看起来像这样。

        aggregate_FID   jurisdiction    FID       name           rate
2217    750             municipal       405       Auburn         0.093
2218    751             municipal       81        Bonney Lake    0.088
2219    752             municipal       405       Auburn         0.093
2220    753             municipal       171       Steilacoom     0.094
2221    754             municipal       235       Lakewood       0.094
2222    755             municipal       176       Fircrest       0.094
2223    750             state           1         Washington     0.065
2224    751             state           1         Washington     0.065

The starting point is a Data Frame with this structure. 起点是具有这种结构的数据框架。

        aggregate_FID   jurisdiction    FID
2217    750             municipal       405
2218    751             municipal       81
2219    752             municipal       405
2220    753             municipal       171
2221    754             municipal       235
2222    755             municipal       176
2223    750             state           1
2224    751             state           1

...and multiple data frames I need to use for populating the name and tax rate fields. ...我需要使用多个数据框来填充名称和税率字段。

    FID name        rate    jurisdiction
0   1   Waterville  0.082   municipal
1   2   Riverside   0.081   municipal
2   3   Pierce HBZ  0.079   municipal
3   4   Cle Elum    0.080   municipal
4   5   Pacific     0.095   municipal

    FID name        rate    jurisdiction
0   1   Washington  0.065   state

I need to match up the latter data frames with the first based on the jurisdiction and FID columns, and populate the name and rate columns. 我需要根据jurisdictionFID列匹配后面的数据框和第一个数据框,并填充namerate列。 I have managed to create a single data frame merging with one of the latter data frames using... 我已设法创建一个单独的数据帧与后一个数据帧合并使用...

df_merge = pd.merge(left=df_aggregate, right=df_jurisdiction, how='left', on=['FID', 'jurisdiction'])

...but this only works for one of the tables. ...但这只适用于其中一个表格。 Unfortunately I need to do this for as little as one, but as many as seven tables. 不幸的是,我需要这样做只需一个,但多达七个表。 This has been a pain for over two days now. 这已经过了两天多的痛苦。 Please feel free to ask for more clarification if I have not been clear enough in my question, and thank you in advance for your help. 如果我的问题不够清楚,请随时要求更多说明,并提前感谢您的帮助。

You can concatenate all the jurisdiction tables first and then use merge . 您可以先连接所有管辖权表,然后使用merge It would look something like this. 它看起来像这样。

j_all = pd.concat([j1, j2, j3, j4, j5, j6, j7])
df_merge = pd.merge(left=df_aggregate, right=j_all, how='left', on=['FID', 'jurisdiction'])

暂无
暂无

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

相关问题 如何使用 pandas dataframe 连接两个列值在多个列的特定范围内的数据框? - how to join two dataframes for which column values are within a certain range for multiple columns using pandas dataframe? 将来自多个pandas数据框的所有列连接到一个包含数据和列名称的数据框 - Join all columns from multiple pandas dataframes into one dataframe with data and column names 如果一个列与一组其他列匹配,则将两个 DataFrame 与 Pandas 连接起来 - Joining two DataFrames in with Pandas if one column matches a set of other columns Pandas 连接两个数据帧,两个数据帧中具有相同索引的列彼此相邻 - Pandas join two dataframes, columns with the same index from the two dataframes are next to each other 合并两个熊猫数据帧,其中一个是另一个的子集(或仅填充列的子集) - Merge two pandas dataframes where one is a subset of the other (or populate only a subset of columns) Pandas DataFrame 从其他 DataFrame 添加两列 - Pandas DataFrame add column by two columns from other DataFrame pandas:在 dataframe 中根据一列中的相似值填充来自多个数据帧的值的空列 - pandas: populate an empty column in a dataframe with values from multiple dataframes based on similar values in one column 合并或连接三个 DataFrames,其中所有三个 DataFrames 包含四个相同的列和一个唯一的列 - Merging or Join three DataFrames where all three DataFrames contain four identical columns and one unique column 使用其他两列对熊猫数据框中的列进行排序 - Sorting a column in pandas dataframe using two other columns 合并两个具有共同值的 Pandas 数据帧,这些值在一个数据帧中显示为列,而在另一个数据框中显示为行 - Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM