很难找出如何用另一个dataframe中的值替换主dataframe的过滤(在本例中为空)值,其中我在它们之间有相同的列/键。
这就是我所拥有的:
我需要填充空白马铃薯值的主数据框:date banana potato avocado
33 2017-06-01 55ee4cbc 80660 b57c-2473556952a8
34 2017-06-01 391dc0f6 82ff-46de03510afc
35 2017-06-01 3a1f407f 54408 a3a6-d85429eef303
36 2017-06-01 3a1f407f 54408 858d-48082acc66ed
37 2017-06-01 5222ab45 80693 ba1f-dbd387748b71
38 2017-06-01 5222ab45 80693 b085-99d58875084a
39 2017-06-01 5222ab45 80693 a570-6d4c766ff7cf
40 2017-06-01 6939ced3 960c-a9ded8ed2f56
41 2017-06-01 2478913a 80610 9258-df6d26027d18
42 2017-06-01 2478913a 80610 8e53-a1d8d4e175b9
43 2017-06-01 2478913a 80610 b4b2-a9221895f8b5
根据匹配的banana值,香蕉土豆匹配了主df中的空格(对马铃薯指数0和3感兴趣):
^{pr2}$
希望以这样的结局:
通过保留相同的索引,或者重置索引以包含来自主数据帧的所有值date banana potato avocado
33 2017-06-01 55ee4cbc 80660 b57c-2473556952a8
34 2017-06-01 391dc0f6 80686 82ff-46de03510afc
35 2017-06-01 3a1f407f 54408 a3a6-d85429eef303
36 2017-06-01 3a1f407f 54408 858d-48082acc66ed
37 2017-06-01 5222ab45 80693 ba1f-dbd387748b71
38 2017-06-01 5222ab45 80693 b085-99d58875084a
39 2017-06-01 5222ab45 80693 a570-6d4c766ff7cf
40 2017-06-01 6939ced3 55031 960c-a9ded8ed2f56
41 2017-06-01 2478913a 80610 9258-df6d26027d18
42 2017-06-01 2478913a 80610 8e53-a1d8d4e175b9
43 2017-06-01 2478913a 80610 b4b2-a9221895f8b5
我尝试过的
如果我合并它,那么我会得到一个新的框架,我不知道如何分配回原来的框架pd.merge(linear_df[linear_df.potato== ''], banana_potato_matched_df, how='left', on='banana')
date banana potato_x potato_y avocado
0 2017-06-01 55ee4cbc NaN b57c-2473556952a8
1 2017-06-01 391dc0f6 80686 82ff-46de03510afc
2 2017-06-01 3a1f407f NaN a3a6-d85429eef303
3 2017-06-01 3a1f407f NaN 858d-48082acc66ed
4 2017-06-01 5222ab45 NaN ba1f-dbd387748b71
5 2017-06-01 5222ab45 NaN b085-99d58875084a
6 2017-06-01 5222ab45 NaN a570-6d4c766ff7cf
7 2017-06-01 6939ced3 55031 960c-a9ded8ed2f56
8 2017-06-01 2478913a 80610 9258-df6d26027d18
9 2017-06-01 2478913a 80610 8e53-a1d8d4e175b9
10 2017-06-01 2478913a 80610 b4b2-a9221895f8b5Other way I think of is to split my main_df to 2 dataframes. One with
blank values (blank_main_df) and another with filled values
(filled_main_df). To merge blank_main_df with banana_potato_matched_df on banana key
and append to filled_main_df. Is that the best way ?