抱歉,如果这已经被问到和答复,但已经搜索了一整天,但无法找到正确的解决方案。如果解决方案已经存在,请给我指一下
我正在尝试在pandas数据帧(df1)的一列中填充na/nan值。填充值位于另一个数据帧(df2)中,其中包含唯一id和相应的值。如何匹配df1.Prod\u id的id(其中df.item\u wt中的现有值是nan),然后在df2.mean\u wt中找到相应的值,并在df1.item\u wt中填充nan值。这两个数据帧的大小不同,df1是80k+行,df2只有1559。列名也不同,因为来自不同的源。填充必须到位
如果能避免给定数据帧大小的迭代循环,我们将不胜感激
我已经尝试过使用combine\ u first和map,但没有成功,因为数据帧大小不同,所以多余的行不会被替换
data1 = {'Prod_id':['PR1', 'PR2', 'PR3', 'PR4', 'PR2', 'PR3','PR1', 'PR4"],store=['store1','store2','store3','store6','store3','store8','store45','store23']'item_wt':[28,nan,29,42,nan,34,87,nan]}
df1 = pd.DataFrame(data1)
data2 = {'Item_name':['PR1', 'PR2', 'PR3', 'PR4'],'mean_wt':[18,12,22,9]}
df2 = pd.DataFrame(data2)
final df should be like:
data1 = {'Prod_id':['PR1', 'PR2', 'PR3', 'PR4', 'PR2', 'PR3','PR1', 'PR4"],store=['store1','store2','store3','store6','store3','store8','store45','store23']'Item_wt':[28,12,29,42,12,34,87,9]}
df1 = pd.DataFrame(data1)
您可以使用^{} 并设置由
values
创建的numpy数组,因为原始序列和新序列的索引不同:或者先使用^{} :
相关问题 更多 >
编程相关推荐