简体   繁体   English

根据第二列中存在的字符串更新第一列

[英]Update 1st column based on string present in 2nd column

I want to update price 2 times if there is "buy 1 get 1 50%" and 3 times if "buy 1 get 1 40%"如果有“买 1 送 1 50%”和 3 次如果“买 1 送 1 40%”,我想更新价格 2 次

  1. Price|价格| Special_Offer特价_优惠
  2. 330 |第330话BUY 1 GET 1 50%买 1 送 1 50%
  3. 810 | 810 | BUY 1 GET 1, 40%买 1 送 1, 40%
  4. 210 |BUY 1,GET 1 at 50% 210 |买一送一 50%

Below is my code, but it is not working下面是我的代码,但它不起作用

DF["Price"]=np.where(DF["Special Offer"].str.contains(r"(BUY 1)(GET1)(50%)",case=False,regex=True),DF["Price]*2,DF["Price])

You forgot the spaces in between, and also dont group with brackets.你忘记了中间的空格,也不用括号分组。 As I undestand you want to capture the price.正如我不明白的那样,您想获取价格。 So we capture it with \\d+ and check if there are needed context with a lookahead like (?= \\| BUY 1 GET 1 50%)所以我们用 \\d+ 捕获它并检查是否有需要的上下文,例如 (?= \\| BUY 1 GET 1 50%)

\d+(?= ?\| ?BUY 1[, ] ?GET 1(?:,? ?| at )50%)
\d+(?= ?\| ?BUY 1[, ] ?GET 1(?:, ?| at )40%)

demo演示

https://regex101.com/r/vG1kzW/3 https://regex101.com/r/vG1kzW/3

https://regex101.com/r/QImMZf/2 https://regex101.com/r/QImMZf/2

this one is working.这个正在工作。

\d+(?= ?\| ?BUY 1[, ] ?GET 1(?:,? ?| at )50%)
\d+(?= ?\| ?BUY 1[, ] ?GET 1(?:, ?| at )40%)

暂无
暂无

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

相关问题 Python Dataframe: To get a column value from 2nd dataframe based on a column in the 1st dataframe is in between two columns in the 2nd dataframe - Python Dataframe: To get a column value from 2nd dataframe based on a column in the 1st dataframe is in between two columns in the 2nd dataframe 根据第一列中的最大日期按月创建第二列 - Create a 2nd column based on the maximum date By Month in 1st column 如何根据第一列,第二列等对Theano中的行进行排序 - How to sort rows in Theano based on 1st column, then 2nd column, etc 合并多个文件:第一列(相同的字符串),第二列(每个文件的唯一值) - Merge multiple files: 1st Column (same string), 2nd Column (unique values per file) 读取panda dataframe的第1列、第2列、第n列到最后一列 - Read 1st column, 2nd column, and nth column to last column of panda dataframe 如何阅读第 1 列和第 2 列,以及第 n 列到最后一列。 Numpy - How to read 1st column and 2nd column, and nth column to last column . Numpy 如何根据第一列将逻辑从(假到真)和基于第二列的逻辑(真到假)更改 - How can I change logic from (False to True) based on the 1st column and (True to False) based on the 2nd culumn Select a value from a 1 DataFrame, based on conditions from 2nd DataFrame and paste this value a new column in the 1st Dataframe - Select a value from a 1 DataFrame , based on conditions from 2nd DataFrame and paste this value a new column in the 1st Dataframe 比较N个文件的第一列,如果找到匹配项,则打印其余文件的第一列和第二列 - Comparing the 1st column of N files, if match found print the 1st file and 2nd column of remaining files 首先按第 1 列然后按第 2 列对 2D 列表进行排序 - sort a 2D list first by 1st column and then by 2nd column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM