如何使两个 pandas 数据帧的一部分相等

How to make part of two pandas dataframe equal

我正在尝试根据 4 个条件将相同大小(1000000 行和 8 列)的两个数据帧(static_df_1 和 static_df_2)的一部分设置为彼此相等。但是,我无法使它们相等。 i 和 j 是每个数据框中的两列,销售额也是这两个数据框中的另一个共享列。我的条件是仅将两个数据帧中 25 < i < 36 和 25 < j < 36 的部分设置为相等。当我执行以下代码时,它们仍然不同且不相等!

             old_sales = static_df_1.loc[(static_df_1['i'] > 25 ) & (static_df_1['i'] < 36) & (static_df_1['j'] > 25 ) & (static_df_1['j'] < 36 )]['sales']

            static_df_2.loc[(static_df_2['i'] > 25 ) & (static_df_2['i'] < 36) & (static_df_2['j'] > 25 ) & (static_df_2['j'] < 36 )]['sales'] = old_sales

通常你会索引

df.loc[row_indexer,column_indexer]

也许将事情分开以便更容易评估。

row_indexer = (static_df_1['i'] > 25 ) & (static_df_1['i'] < 36) & (static_df_1['j'] > 25 ) & (static_df_1['j'] < 36 )
old = static_df_1.loc[row_indexer,'sales']
static_df_2.loc[row_indexer,'porosity'] = old

我这里没有安装Pandas所以无法测试。


来自 Pandas 用户指南中的 Boolean indexing(强调我的):

With the choice methods Selection by Label, Selection by Position, and Advanced Indexing you may select along more than one axis using boolean vectors combined with other indexing expressions.

也来自Different choices for indexing

.loc is primarily label based, but may also be used with a boolean array