简体   繁体   English

如何有条件地将子字符串复制到pandas数据帧的新列?

[英]How to conditionally copy a substring into a new column of a pandas dataframe?

This is my first post so hopefully I don't botch the question and I'm clear. 这是我的第一篇文章,所以希望我不要讨论这个问题,我很清楚。 Basically, this is a two part question. 基本上,这是一个两部分问题。 I need to set up code that first checks whether or not Column A = "VALID". 我需要设置代码,首先检查列A是否为“有效”。 If this is true, I need to extract the substring from column B and place it in a new column, here labeled "C". 如果是这样,我需要从列B中提取子字符串并将其放在一个新列中,此处标记为“C”。 If the conditional is false, I will like to put in "NA". 如果条件为假,我想加入“NA”。 See the second table for my desired outcome. 请参阅第二个表格以了解我的预期结果。

|     A       |             B                     |
|-------------|-----------------------------------|
|    VALID    |asdfafX'XextractthisY'Yeaaadf      |
|    INVALID  |secondrowX'XsubtextY'Yelakj        |
|    VALID    |secondrowX'XextractthistooY'Yelakj |

|     A       |             B                       |      C          |
|-------------|-------------------------------------|-----------------|
|    VALID    |"asdfafX'XextractthisY'Yeaaadf"      | extractthis     |
|    INVALID  |"secondrowX'XsubtextY'Yelakj"        | NA              |
|    VALID    |"secondrowX'XextractthistooY'Yelakj" | extractthistoo  |

A few things to note: 有几点需要注意:

-The substring will always start after the phrase "X'X" and finish right before "Y'Y". - 子字符串将始终在短语“X'X”之后开始,并在“Y'Y”之前完成。

-The substring will be of differing lengths from cell to cell. - 子串在不同的单元格之间具有不同的长度。

I know the following code is wrong, but I wanted to show you at how I have been attempting to solve this problem: 我知道以下代码是错误的,但我想告诉你我是如何尝试解决这个问题的:

import pandas as pd

if df[A] == "VALID":
   df[C] = df[B]df.str[start:finish]
else:
   df[C].isna()

I apologize for the errors in this basic code, as I am new to python altogether and still rely on an IDE and trial&error to guide me. 我为这个基本代码中的错误道歉,因为我完全是python的新手,仍然依赖IDE和试错来指导我。 Any help you can provide is appreciated. 您可以提供的任何帮助表示赞赏。

You can use pd.Series.str.extract : 您可以使用pd.Series.str.extract

In [737]: df
Out[737]: 
         A                                   B
0    VALID       asdfafX'XextractthisY'Yeaaadf
1  INVALID         secondrowX'XsubtextY'Yelakj
2    VALID  secondrowX'XextractthistooY'Yelakj

In [745]: df['C'] = df[df.A == 'VALID'].B.str.extract("(?<=X'X)(.*?)(?=Y'Y)", expand=False)

In [746]: df
Out[746]: 
         A                                   B               C
0    VALID       asdfafX'XextractthisY'Yeaaadf     extractthis
1  INVALID         secondrowX'XsubtextY'Yelakj             NaN
2    VALID  secondrowX'XextractthistooY'Yelakj  extractthistoo

The regex pattern is: 正则表达式模式是:

(?<=X'X)(.*?)(?=Y'Y)
  • (?<=X'X) is a lookbehind for X'X (?<=X'X)X'X

  • (.*?) matches everything between the lookbehind and lookahead (.*?)匹配lookbehind和lookahead之间的所有内容

  • (?=Y'Y) is a lookahead for Y'Y (?=Y'Y)Y'Y

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

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