使用 if 语句查找特定值 [英] Looking for specific value with if statement

查看:42
本文介绍了使用 if 语句查找特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 excel 中有一个很大的产品订单数据集(它会继续增长).

Hi I have a large dataset in excel (and it will keep growing) of products' orders.

数据集看起来像这样

Product   Date         Lsat24               Next24                  Summary                   Buyer 
*day1*
AX1  |  2/1/2019 |Checking inventory  |Invoicing             |The product request started today |  HTN
X5L  |  2/1/2019 |Inventory counting  |Future request planing|No operation update               | null
NO6  |  2/1/2019 |Request in-process  |Closing request       |Operation product request ended   |  KLW
ZS5  |  2/1/2019 |Order placed        |Processing request    |Request started for the product   |  WHM
*day2* 
AX1  |  3/1/2019 |Invoicing the order |Logistic planning     |Product requested still pending   |  HTN
X5L  |  3/1/2019 |New request planing |processing order      |A new product request started     |  LKJ
NO6  |  3/1/2019 |Closing request     |No new request        |No update for new operation       |  null
ZS5  |  3/1/2019 |Request ended       |New order placed      |Product inventory counting        |  WHM
*day3*
AX1  |  4/1/2019 |Logistic planning   |Closing the request   |Product request ended             |  HTN
X5L  |  4/1/2019 |Request processing  |Planning logistic     |The product request under-process |  LKJ
NO6  |  4/1/2019 |No update           |New Request Planing   |No update for new operation       |  null
ZS5  |  4/1/2019 |New order started   |Request invoicing     |Inventory and logistic planing    |  KLW

我的最终目标是获得每个买家订单的百分比.这意味着我需要在 3 列(La​​st24、Next 24 和 Summary)中寻找世界所以为了得到这个,我需要确保 if Product == Product and Buyer == Buyer then 搜索 Last24 or Next24 字符串已结束"的摘要

My ultimate goal is to get the percentage of each buyer orders. And that would mean I need to look for the world in 3 columns (Last24, Next 24 and Summary) So to get that I need to make sure that if Product == Product and Buyer == Buyer then search Last24 or Next24 or Summary for string 'ended'

结果看起来像这个表格

Product   Date         Lsat24               Next24                      Summary                   Buyer 
*day1*
NO6  |  2/1/2019 |Request in-process  |Closing request       |Operation product request ended   |  KLW
*day2* 
ZS5  |  3/1/2019 |Request ended       |New order placed      |Product inventory counting        |  WHM
*day3*
AX1  |  4/1/2019 |Logistic planning   |Closing the request   |Product request ended             |  HTN

所以真实的数据表(在excel中)有200多个产品.这些产品将每天重复.每个产品的 Last24、Next24 和摘要每天都不同.买家也可以更改(并非总是如此).

So the real data table (in excel) has over 200 products. These products will be repeated everyday. The Last24, Next24 and summary for each product is different every day. Also the buyer can change(Not always).

我使用的是 Python3.8.

I am using Python3.8.

编辑"

为了使我的问题更清楚,我将使用示例.

To make my question clear I will use examples.

  Product   Date         Lsat24               Next24                  Summary                   Buyer 
*day1*
0  AX1  |  2/1/2019  |Checking inventory  |Invoicing            |The product request started today |  HTN
1  X5L  |  2/1/2019  |Inventory counting |Future request planing|No operation update               | null
2  NO6  |  2/1/2019 |Request in-process  |Closing request       |Operation product request ended   |  KLW
3  ZS5  |  2/1/2019 |Order placed        |Processing request    |Request started for the product   |  WHM
*day2* 
4  AX1  |  3/1/2019 |Invoicing the order |Logistic planning     |Product requested still pending   |  HTN
5  X5L  |  3/1/2019 |New request planing |processing order      |A new product request started     |  LKJ
6  NO6  |  3/1/2019 |Closing request     |No new request        |No update for new operation       |  null
7  ZS5  |  3/1/2019 |Request ended       |New order placed      |Product inventory counting        |  WHM
*day3*
8  AX1  |  4/1/2019 |Logistic planning   |Closing the request   |Product request ended             |  HTN
9  X5L  |  4/1/2019 |Request processing  |Planning logistic     |The product request under-process |  LKJ
10 NO6  |  4/1/2019 |No update           |New Request Planing   |No update for new operation       |  null
11 ZS5  |  4/1/2019 |New order started   |Request invoicing     |Inventory and logistic planing    |  KLW

一个案例

*if** Product == Product ([1] X5L == [5] X5L) 然后选择 Buyer == Buyer ([1] null != [5] LKJ(因为它们不匹配) 搜索行 [1] 表示 Last24 Next24 摘要中的结束",如果在 3 列中的至少一列中找到结束",则返回行 [1] 否则删除它)

*if** Product == Product ([1] X5L == [5] X5L) then go for Buyer == Buyer ([1] null != [5] LKJ(since they do not match) search in row [1] for "ended" in Last24 or Next24 or Summary, if "ended" is found in at least one of the 3 columns return the row[1] else remove it)

另一种情况

*if** Product == Product ([0] AX1 == [4] AX1),然后选择 Buyer == Buyer ([0]HTN == [4]HTN,因为它们匹配,所以选择下一个具有相同产品名称的行.产品 == 产品 ([4]AX1 ==[8] AX1),然后买方 == 买方 ([4]HTN == [8]HTN),因为它们匹配并且没有具有相同产品名称的新行,搜索第 [8] 行表示 Last24 Next24 摘要中的结束").

*if** Product == Product ([0] AX1 == [4] AX1), then go for Buyer == Buyer ([0]HTN == [4]HTN, since they match go for the next row with the same product name. Product == Product ([4]AX1 ==[8] AX1), then Buyer == Buyer ([4]HTN == [8]HTN), since they match and no new row with the same product name, search in row [8] for "ended" in Last24 or Next24 or Summary).

有点复杂,我知道.

推荐答案

我认为你需要 Series.str.endswith 用于使用 DataFrame.any 用于测试至少一个 True - 每行一个匹配项并按 过滤布尔索引:

I think you need Series.str.endswith for looking for string in 3 columns with DataFrame.any for test at least one True - one match per row and filter by boolean indexing:

cols = ['Lsat24','Next24','Summary']
df = df[df[cols].apply(lambda x : x.str.endswith('ended')).any(axis=1)]

print (df)
  Product      Date              Lsat24               Next24  \
2     NO6  2/1/2019  Request in-process      Closing request   
7     ZS5  3/1/2019       Request ended     New order placed   
8     AX1  4/1/2019   Logistic planning  Closing the request   

                           Summary Buyer  
2  Operation product request ended   KLW  
7       Product inventory counting   WHM  
8            Product request ended   HTN  

这篇关于使用 if 语句查找特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆