添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Considering the below dataframes:

df = pd.DataFrame([["11","1", "2"], ["12","1", "2"], ["13","3", "4"]],
                 columns=["ix","a", "b"])
df1 = pd.DataFrame([["22","8", "9"], ["12","10", "11"], ["23","12", "13"]],
                     columns=["ix","c", "b"])
df                     df1
    ix  a   b        ix  c   b
0   11  1   2     0  22  8   9
1   12  1   2     1  12  10  11
2   13  3   4     2  23  12  13

if we execute df.update(df1) , this will update the entire column ix & b of dataframe -df since the index number for both dataframes are same.

However, I was trying to set the ix column as index for both the dataframes and trying to update the first one as shown below:

df_new = df.set_index('ix').rename_axis(None).update(df1.set_index('ix').rename_axis(None))

However, this does not return anything.

I was expecting this to return a dataframe with column b updated for df where ix of df1 and df matches. Something like:

    a   b
11  1   2
12  1   11
13  3   4

Am I missing something here? Is df.update() is not meant for executing in a copy of a dataframe? Can anyone please explain me why is this happening.

Yes, update is an in-place operation that returns nothing. You will need to assign the set_index result before calling update, and then reset_index after. – cs95 Jan 5, 2019 at 6:34 df.set_index('ix').rename_axis(None) returns a copy, to which you call update. update returns None, and the copy, having been updated, no longer has any references and is eventually garbage collected. – cs95 Jan 5, 2019 at 6:39 I'm guessing you don't want to update the original? In that case, just make a copy: df_copy = df.set_index('ix'); df_copy.update(df1.set_index('ix')) – cs95 Jan 5, 2019 at 6:43

Modify in place using non-NA values from another DataFrame.

Aligns on indices. There is no return value.

So, your only option is to set the index as a separate step beforehand.

df.set_index('ix', inplace=True)
df.update(df1.set_index('ix'))
df.reset_index()
   ix  a   b
0  11  1   2
1  12  1  11
2  13  3   4

If you are trying to avoid modifying the original, this is always another option:

df_copy = df.set_index('ix')
df_copy.update(df1.set_index('ix'))
df_copy
    a   b
11  1   2
12  1  11
13  3   4
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.