【问题标题】:Get latest row from csv based on custom condition根据自定义条件从 csv 获取最新行
【发布时间】:2020-07-30 22:26:02
【问题描述】:

我有一张桌子material

+--------+-----+-------------------+----------------+-----------+          
| ID     | REV | name              | Description    | curr      |
+--------+-----+-------------------+----------------+-----------+
| 211-32 | 001 | Screw 1.0         | Used in MAT 1  | READY     |
| 211-32 | 002 | Screw 2 plus      | can be Used-32 | WITHDRAWN |
| 212-41 | 001 | Bolt H1           | Light solid    | READY     |
| 212-41 | 002 | BOLT H2+Form      | Heavy solid    | READY     |
| 101-24 | 001 | HexHead 1-A       | NOR-1          | READY     |
| 101-24 | 002 | HexHead Spl       | NOR-22         | READY     |
| 423-98 | 001 | Nut Repair spare  | NORM1          | READY     |
| 423-98 | 002 | Nut Repair Part-C | NORM2          | WITHDRAWN |
| 423-98 | 003 | Nut SP-C          | NORM2+NORM1    | NULL      |
| 654-01 | 001 | Bar               | Specific only  | WITHDRAWN |
| 654-01 | 002 | Bar rod-S         | Designed+Spe   | WITHDRAWN |
| 654-01 | 003 | Bar OPG           | Hard spec      | NULL      |
+--------+-----+-------------------+----------------+-----------+

这里每个 ID 可以有多个修订。我想采用最新的修订版(即最高的 001,002,003 等)。但是如果最新版本的currNULL(string) 或WITHDRAWN,那么我已经采用了以前的版本及其相应的值。如果即使是 currNULLWITHDRAWN 我必须再次转到以前的版本。如果所有版本都有相同的问题,那么我们可以忽略它。所以预期的输出是

+--------+-----+------------------+---------------+-------+
| ID     | REV | name             | Description   | curr  |
+--------+-----+------------------+---------------+-------+
| 211-32 | 001 | Screw 1.0        | Used in MAT 1 | READY |
| 212-41 | 002 | BOLT H2+Form     | Heavy solid   | READY |
| 101-24 | 002 | HexHead Spl      | NOR-22        | READY |
| 423-98 | 001 | Nut Repair spare | NORM1         | READY |
+--------+-----+------------------+---------------+-------+

我对 Python 很陌生。我试过下面的代码,但我不工作。任何建议都非常感谢。

import pandas as pd
import numpy as np

mydata = pd.read_csv('C:/Myfolder/Python/myfile.csv')

mydata.sort_values(['ID','REV'], ascending=[True, False]).drop_duplicates('',keep=last)

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我们可以创建一个伪列来获取最大值并返回它的索引。

    第一步是过滤掉我们想要忽略的值。

    df1 = df.loc[
        df[~df["curr"].isin(["WITHDRAWN", "NULL"])]
        .assign(key=df["REV"].astype(int))
        .groupby("ID")["key"]
        .idxmax()
    ]
    
    
             ID  REV                 name       Description   curr
    6   101-24   002   HexHead Spl          NOR-22           READY
    1   211-32   001   Screw 1.0            Used in MAT 1    READY
    4   212-41   002   BOLT H2+Form         Heavy solid      READY
    7   423-98   001   Nut Repair spare     NORM1            READY
    

    【讨论】:

    • 非常感谢。我看到你来自 SQL 背景。 :) 我认为你是回答我问题的最佳人选,因为我是一名 SQL 开发人员并且是 python 新手。 1) .loc 是否类似于 where 条件? 2).astype 等价于cast? 3) 我在哪里可以学习 pandas 中的复制 SQL 函数。
    • CAST 确实类似于 astype 但熊猫还有其他功能,例如 pd.to_numeric 就像 COALESCE 我会说 .loc 是熊猫版本的按索引和列过滤,我想它的等价物是 WHERE
    • 感谢您的提示。最后一个问题,groupby("ID")["key"] 是什么?我可以写成groupby("ID","key")吗?这里有点困惑
    • 啊。如果我的理解是正确的,那么["key"] .idxmax() = MAX("Key") ?
    • 太棒了。再次感谢您!
    【解决方案2】:

    您可以使用isin 选择其中没有NULL 或 WITHDRAW 的行,然后执行sort_valuesdrop_duplicates

    mydata = mydata[~mydata['curr'].isin(['NULL','WITHDRAW'])]
    mydata = mydata.sort_values(['ID','REV']).drop_duplicates('ID',keep='last')
    

    【讨论】:

      【解决方案3】:

      我认为你首先应该从表中删除 NULL 或 WITHDRAW。

      mydata[mydata[curr] == 'Ready']       # this should do I think...
      

      然后您可以尝试排序并获取最大转速值。

      mydata = mydata.sort_values(['ID','REV']).drop_duplicates('ID',keep='last')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        • 2023-04-03
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多