首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何更新PythonPandasDataFrame中特定行中的值?

如何更新PythonPandasDataFrame中特定行中的值?

提问于 2018-02-28 03:59:49
回答 2关注 0查看 8.3K

在Pandas中有很好的索引方法,我不会以各种方式提取数据。另一方面,我仍然对如何更改现有DataFrame中的数据感到困惑。

在下面的代码中,我有两个DataFrame,我的目标是从第二个df的值更新第一个df中特定行的值。我怎样才能做到这一点?

代码语言:txt
复制
import pandas as pd
df = pd.DataFrame({'filename' :  ['test0.dat', 'test2.dat'], 
                                  'm': [12, 13], 'n' : [None, None]})
df2 = pd.DataFrame({'filename' :  'test2.dat', 'n':16}, index=[0])

# this overwrites the first row but we want to update the second
# df.update(df2)

# this does not update anything
df.loc[df.filename == 'test2.dat'].update(df2)

print(df)

返回

代码语言:txt
复制
   filename   m     n
0  test0.dat  12  None
1  test2.dat  13  None

[2 rows x 3 columns]

但我怎样才能做到这一点:

代码语言:txt
复制
    filename   m     n
0  test0.dat  12  None
1  test2.dat  13  16

[2 rows x 3 columns]

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档