我可以使用布尔循环找到两个df cols相等的值,然后设置df1 ['col1'] = df2 ['col2']

时间:2019-08-22 12:28:10

标签: python pandas loops iterator

我必须找到等于df2 col1的{​​{1}}的值,然后在同一行中用df1 col1替换df1 col2

我已经尝试过df2 col2(可能不正确)并且遇到了多种条件,即.isin()

if (df1['col1'] == df2['col1']) & (df1['col3'] == 'x index')

2 个答案:

答案 0 :(得分:0)

我是熊猫的新手,但是具有Pythonic选项:

CLOSE

from pandas import DataFrame as DF columns = ["col1", "col2"] df1 = DF([ (1, "a"), (2, "b"), (3, "c") ], columns=columns) df2 = DF([ (4, "x"), (2, "y"), (5, "z") ], columns=columns) for i, z in enumerate(zip(df1.col1, df2.col1, df2.col2)): compare_1, compare_2, value = z if compare_1 == compare_2: df1.col2[i] = value pass pass print(df1) # col1 col2 # 0 1 a # 1 2 y <--- new value # 2 3 c 的解释:

枚举生成enumerate的元组

(index, value_from_list)

for i, value in enumerate(["x", "y", "z"]): print(i, value, sep=": ") # Output: # 0: x # 1: y # 2: z 的解释:

Zip生成可迭代值(列表,字典等)中每个值的元组。

zip

答案 1 :(得分:0)

请,如果您找到不使用循环的解决方案,那总是更好。在您的情况下,可以通过内部联接来解决查找另一列中的行的问题。我希望这里是可以解决您问题的代码。

In [1]:
## Set the exemple with replicable code
import pandas as pd

cols = ['col1', 'col2'] 
data = [[100, 150],
        [220, 240],
        [80, 60]
       ]

df1 = pd.DataFrame(data=data, columns=cols).set_index('col1')


cols = ['col1', 'col2'] 
data = [[111, 0],
        [220, 0],
        [80, 0]
       ]

df2 = pd.DataFrame(data=data, columns=cols).set_index('col1')

## Get all the rows from df1 col1 that are in df2 col1
df_merge = df1.merge(df2, left_index=True, right_index=True, how='inner', suffixes=('_df1', '_df2'))
df_merge

Out [1]:
        col2_df1    col2_df2
col1        
220     240         0
80      60          0

然后进行左连接以将col2 df2col2 df1的值相加

In [2]:
df1 = df1.merge(df_merge, how='left', left_index=True, right_index=True)
df1.drop(axis=1, columns=['col2', 'col2_df1'], inplace=True)
df1.rename(columns={'col2_df2': 'df2'}, inplace=True)
df1

Out [2]:
    df2
col1    
100 NaN
220 0.0
80  0.0