繁体   English   中英

如何根据另一个数据框中的列的存在来更新数据框中列的值

[英]How can I update values of a column in a dataframe based on its existence in another dataframe

import pandas as pd
import numpy as np
from numpy.random import randint
from IPython.display import display, HTML
dict_1 = {'col1':range(0,21),'col3':0}
dict_2 = {'col2':range(0,41,4)}
df = pd.DataFrame(dict_1)
df_2 = pd.DataFrame(dict_2)

因此,目标是比较df_2['col2']df['col1']中的所有值,并且对于等于另一行的每一行,我需要将 col3 更新为 1 或除现在之外的某个值

在这种情况下,我正在寻找类似的东西:

df = pd.DataFrame({'col1':[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20],'col3':[1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1]}

一种选择是isin + np.where

df['col3'] = np.where(df['col1'].isin(df_2['col2']), 1, 0)

df.head() :

    col1  col3
0      0     1
1      1     0
2      2     0
3      3     0
4      4     1

编辑包括col4merge和使用np.where根据其中设定值col4 (从值df2 )是nan

给定的

df = pd.DataFrame({'col1': range(0, 21), 'col3': 0})
df_2 = pd.DataFrame({'col2': [0, 4, 8, 12, 16],
                     'col4': ['a', 'b', 'c', 'd', 'e']})
df = df.merge(
    df_2, left_on='col1', right_on='col2', how='left'
).drop(columns='col2').fillna('')
df['col3'] = np.where(df['col4'].isna(), 0, 1)

df.head() :

    col1  col3 col4
0      0     1    a
1      1     0     
2      2     0     
3      3     0     
4      4     1    b

你可以直接用熊猫来做:

df_1['col3'] = df_1['col1'].isin(df_2['col2']).astype(int)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM