首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将来自不同列的两个值连接到单个列中

如何将来自不同列的两个值连接到单个列中
EN

Stack Overflow用户
提问于 2019-05-23 04:05:47
回答 2查看 78关注 0票数 -1

我有dataframe,其中有两个字符串列,这两个列需要连接到单个列

在2列中有3个值。

1.列Comment_vol由Blank、Pass和VolA组成

2. 2.Column Comment_wt由wtA、Pass组成

现在我需要一个专栏,

  1. 当Comment_vol列中有空值,并且Comment wt列中有任何值时,它应该取comment_wt列中的值,反之亦然当两个列值都有Pass时,它应该取Pass
  2. ,如果同时有VolA和wtA,它应该取两个

输入:

代码语言:javascript
复制
  Comment_vol    Comment_wt     
  Pass           wtA            
                 Pass            
  VolA           Pass           
  Pass           Pass           
                 wtA            
  VolA           wtA  

输出:

代码语言:javascript
复制
  Comment_vol    Comment_wt     Comment_final
  Pass           wtA            wtA
                 Pass           Pass 
  VolA           Pass           VolA
  Pass           Pass           Pass
                 wtA            wtA
  VolA           wtA            VolA, WtA

代码:

代码语言:javascript
复制
 df['Comment'] = df['comment_vol'].str.cat(df['comment_wt'], sep =" ")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-23 07:12:14

代码语言:javascript
复制
def concatcolumns(x):
    vol = str(x[0])
    wt = str(x[1])
    if vol in ['nan', 'Pass']:
        return wt
    elif wt == 'Pass':
        return vol
    else:
        return ", ".join(x)

df['Comment'] = df[['Comment_vol', 'Comment_wt']].apply(lambda x: concatcolumns(x),axis=1)
票数 1
EN

Stack Overflow用户

发布于 2019-05-23 07:36:56

编辑:添加说明

df.Comment_vol.str.strip().isin(['Pass', ''])去掉前面和后面的空格,并使用isin检查列Comment_vol中的值是“Pass”还是“”。我使用strip来确保您的数据包含诸如“Pass”或“VolA”之类的单词(请注意前面和后面的空格),它仍然有效。这将返回一个布尔值系列,'Pass‘或'’时为True,否则为False。将此赋值给n

df.Comment_wt.str.strip().isin(['Pass', ''])相同,但应用于列Comment_wt并赋值给m

'~'是否定运算符,~n表示Comment_vol中既不'Pass‘也不’的任何单词

np.select([n, ~n & m], [df.Comment_wt, df.Commnt_vol], df.Comment_vol.str.cat(df.Comment_wt, sep=', '))等同于逻辑

代码语言:javascript
复制
if n:
    df.Comment_wt
elif ~n & m: #`Comment_vol` is NOT 'Pass' or '' and  df.Comment_wt is 'Pass' or ''
    df.Commnt_vol
else:
    df.Comment_vol.str.cat(df.Comment_wt, sep=', ') #concat both columns using `,'

np.select返回数组,如下所示:

代码语言:javascript
复制
np.select([n, ~n & m], [df.Comment_wt, df.Comment_vol], df.Comment_vol.str.cat(df.Comment_wt, sep=', '))

Out[350]: array(['wtA', 'Pass', 'VolA', 'Pass', 'wtA', 'VolA, wtA'], dtype=objec
t)

此数组用于创建dfComment_final

您可以阅读np.select的文档以了解更多信息https://docs.scipy.org/doc/numpy/reference/generated/numpy.select.html

原创:

如果我正确理解了您的描述和输出,这是使用np.select的典型案例

代码语言:javascript
复制
n = df.Comment_vol.str.strip().isin(['Pass', ''])
m = df.Comment_wt.str.strip().isin(['Pass', ''])

df['Comment_final'] = np.select([n, ~n & m], [df.Comment_wt, df.Comment_vol], df.Comment_vol.str.cat(df.Comment_wt, sep=', '))


Out[591]:
  Comment_vol Comment_wt Comment_final
0        Pass        wtA           wtA
1                   Pass          Pass
2        VolA       Pass          VolA
3        Pass       Pass          Pass
4                    wtA           wtA
5        VolA        wtA     VolA, wtA
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56264223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档