如何使用python比较同一数据帧中的两列来创建新列?

2024-05-14 03:40:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面显示的数据帧。 数据框:

col_1 col_2 
EDU   facebook
EDU   google
EDU   google_usa
EDU   tabula
EDU   xyz
EDU   abc
IAR   facebook
IAR   google

如果col_1有'EDU',列2有'facebook', 'google'新列应该有相同的字符串,即facebook and google,如果列2包含'google_usa',tabula',则新列应该包含'gusa',如果col 2有任何其他字符串,则在同一数据帧中ne_col应该有others。 如果col_1有'IAR',col_2有{}新列应该有facebook,col_2中的任何其他字符串都应该在同一数据框中包含'other'。在

预期产量:

^{pr2}$

我试过下面的代码,但没用出去,拜托在这方面帮助我。 提前谢谢。在

if df['col_1'].str.contains('EDU').any():

        df['new_col'] = ['facebook' if 'facebook' in x else
                            'google' if 'google' == x else
                            'gcusa_tb' if 'taboola' in x else
                            'gcusa_tb' if 'google_cusa' in x else
                            'Others' for x in df['col_2']]

Tags: 数据字符串indffacebookifgooglecol
3条回答

我相信这是理解代码如何工作的最简单方法,这样您就可以将它应用于更多的情况,而不仅仅是这个例子。这相当直观。你可以边走边加逻辑。在

1)首先我们创建一个函数

2)应用上述功能

def new_col(col):
    if col['col1'] == 'EDU' and col['col2'] == 'facebook':
        return 'facebook'
    if col['col1'] == 'EDU' and col['col2'] == 'google':
        return 'google'
    if col['col2'] == 'google_usa' or col['col2'] == 'tabula':
        return 'gusa'
    if col['col1'] == 'IAR' and col['col2'] == 'facebook':
        return 'facebook'
    return 'others'

df['new_col'] = df.apply(lambda col: new_col (col),axis=1)

输出(我的col1和col2向后。别介意。这样读起来更容易):

^{pr2}$
is_edu = df.col_1 == 'EDU'
g_or_f = df.col_2.isin(['google', 'facebook'])
g_or_t = df.col_2.isin(['google_usa', 'tabula'])
is_iar = df.col_1 == 'IAR'
is_fac = df.col_2 == 'facebook'

df.assign(
    new_col=np.where(
        is_edu,
        np.where(
            g_or_f, df.col_2,
            np.where(g_or_t, 'gusa', 'other')
        ),
        np.where(
            is_iar & is_fac, 'facebook', 'other'

        )
    )
)

  col_1       col_2   new_col
0   EDU    facebook  facebook
1   EDU      google    google
2   EDU  google_usa      gusa
3   EDU      tabula      gusa
4   EDU         xyz     other
5   EDU         abc     other
6   IAR    facebook  facebook
7   IAR      google     other

我会使用几个numpy命令:

df['new_col'] = 'others'
df.loc[np.logical_and(df.col_1=='EDU', np.in1d(df.col_2, ['facebook','google'])), 'new_col'] = df.loc[np.logical_and(df.col_1=='EDU', np.in1d(df.col_2, ['facebook','google'])), 'col_2']
df.loc[np.logical_and(df.col_1=='EDU', np.in1d(df.col_2, ['google_usa','tabula'])), 'new_col'] = 'gusa'

注:你的要求与你提出的结果不完全一致,我希望我已经正确地解释了这个要求。我的代码将输出:

^{pr2}$

相关问题 更多 >