简体   繁体   English

熊猫包含带有替换的公式

[英]Panda contains formula with replace

I cant get if statement to work on 2 columns我无法让 if 语句在 2 列上工作

Progress so far:目前进展:

col1= example.com/abc/abs

Goal: to get output of the whats after last "/"目标:得到output最后一个"/"之后的内容

This worked:这有效:

df['col2'] = df['col1'].str.rsplit('/', 1, expand=True)[1]

I need help with this part:我需要这部分的帮助:

If col2 contains "/" , then I want to replace col1 with it, otherwise leave col1 as is.如果 col2 包含"/" ,那么我想用它替换 col1 ,否则保持 col1 不变。

I tried but did not work:我试过但没有用:

df1['col1'] = np.where(df1['col2'] =="/" , df1['col2'], df1['col1'])

Edit:编辑:

df['col1'] = numpy.where(df['col2'].notna(), df['col2'], df['col1'])

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

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