[英]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. 我需要根据
jurisdiction
和FID
列匹配后面的数据框和第一个数据框,并填充name
和rate
列。 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.