熊猫更新没有任何作用

时间:2019-07-08 11:11:47

标签: python pandas

我有一个数据框,上面有一些信息。我创建了另一个更大且具有默认值的数据框。我想使用第一个数据框中的值更新默认数据框。我正在使用df.update但什么都没有发生。这是代码:

new_df = pd.DataFrame(index=range(25))
new_df['Column1'] = 1
new_df['Column2'] = 2
new_df.update(old_df)

在这里,old_df有2行,索引为5,6,其中Column1和Column2中有一些随机值,而没有其他内容。我期望这些行会覆盖new_df中的默认值,我在做什么错了?

2 个答案:

答案 0 :(得分:0)

这对我有用,因此我认为问题出在您未显示给我们的代码部分。

import pandas as pd
import numpy as np

new_df = pd.DataFrame(index=range(25))

old_df = pd.DataFrame(index=[5,6])

new_df['Column1'] = 1
new_df['Column2'] = 2

old_df['Column1'] = np.nan
old_df['Column2'] = np.nan
old_df.loc[5,'Column1'] = 9
old_df.loc[6,'Column2'] = 7
new_df.update(old_df)

print(new_df.head(10))

输出:

   Column1  Column2
0      1.0      2.0
1      1.0      2.0
2      1.0      2.0
3      1.0      2.0
4      1.0      2.0
5      9.0      2.0
6      1.0      7.0
7      1.0      2.0
8      1.0      2.0
9      1.0      2.0

答案 1 :(得分:0)

由于您没有向我们提供如何构造/获取old_df,在进行更新之前,请确保两个索引的类型相同。

new_df.index = new_df.index.astype('int64')
old_df.index = old_df.index.astype('int64')

一个 int 不等于一个 string 1 != '1'。所以 update() 没有在你的数据框中找到常见的行,也没有什么可做的。

相关问题