【发布时间】:2022-12-03 15:48:15
【问题描述】:
我试图根据两个 DataFrame 中存在的公共列将值从一个 DataFrame 填充到另一个 DataFrame。
我为这个操作写的代码如下:
for i in df1.zipcodes:
for j in df2.zipcodes.unique():
if i == j:
#print("this is i:",i, "this is j:",j)
df1['rent'] = df2['rent']
有问题的 Dataframes (df1) 看起来像形状 (131942, 2):
Providing 1st ten rows of df1:
zipcodes districts
018906 01
018907 01
018910 01
018915 01
018916 01
018925 01
018926 01
018927 01
018928 01
018929 01
018930 01
Additionally, there are no duplicates for the Zipcodes column, but the district column has 28 unique values. No Nan values are present.
另一个 DataFrame(df2) 看起来与形状相同 (77996, 4)
Providing 1st ten rows of df2
street zipcodes district rent
E ROAD 545669 15 3600
E ROAD 545669 15 6200
E ROAD 545669 15 5500
E ROAD 545669 15 3200
H DRIVE 459108 19 3050
H DRIVE 459108 19 2000
A VIEW 098619 03 4200
A VIEW 098619 03 4500
J ROAD 018947 10 19500
O DRIVE 100088 04 9600
Note: The Zipcodes in df2 can repeat.
现在,如果 df1 中的邮政编码与 df2 中的邮政编码匹配,我想在 df1 中填充一个名为 rent 的列。如果邮政编码匹配但 df2 中有多个具有相同邮政编码的条目,那么我想将平均值填充为租金。如果邮政编码只有一个条目,那么我想填充与该邮政编码对应的租金。
对上述任何帮助将不胜感激。
【问题讨论】:
标签: python pandas dataframe average