【问题标题】:How to update a df based in a list. Python如何更新基于列表的 df。 Python
【发布时间】:2023-01-16 23:59:55
【问题描述】:

我有下一个 df:


import pandas as pd

data = [{'car' :'audi','id':'ab','year':2001,'wheel':4},
        {'car' :'honda','id':'aa','year':2002,'wheel':15},
        {'car' :'tesla','id':'aaa','year':2003,'wheel':5}]

keys=['a','aa','aaa','avaa','2ffa']

Car ID Year Status Wheels
Audi ab 2001 OK 4
Honda baaa 2002 OK 4
Tesla aaa 2003 OK 4
Tesla avaa 2023 OK 4

那么,让我们想象下一种情况:密钥列表中的 ID 有缺陷,因此,我必须在适当的情况下将“状态”列更新为“有缺陷”而不是“正常”。

我怎样才能做到这一点?谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    结合使用isinboolean indexing

    df.loc[df['id'].isin(keys), 'Status'] = 'Defective'
    

    更新df

         car   id  year  wheel     Status
    0   audi   ab  2001      4        NaN
    1  honda   aa  2002     15  Defective
    2  tesla  aaa  2003      5  Defective
    

    使用的输入:

    df = pd.DataFrame(data)
    

    【讨论】:

      【解决方案2】:
      In [220]: data = [{'car' :'audi','id':'ab','year':2001,'wheel':4},
           ...:         {'car' :'honda','id':'aa','year':2002,'wheel':15},
           ...:         {'car' :'tesla','id':'aaa','year':2003,'wheel':5}]
           ...:
           ...: keys=['a','aa','aaa','avaa','2ffa']
           ...:
      
      In [221]: df = pd.DataFrame(data)
      
      In [222]: df
      Out[222]:
           car   id  year  wheel
      0   audi   ab  2001      4
      1  honda   aa  2002     15
      2  tesla  aaa  2003      5
      
      In [223]: df['status'] = "OK"
      
      In [224]: df
      Out[224]:
           car   id  year  wheel status
      0   audi   ab  2001      4     OK
      1  honda   aa  2002     15     OK
      2  tesla  aaa  2003      5     OK
      
      
      In [225]: for k in keys:
           ...:     df.loc[df.id==k, 'status'] = "Defective"
           ...:
      
      In [226]: df
      Out[226]:
           car   id  year  wheel     status
      0   audi   ab  2001      4         OK
      1  honda   aa  2002     15  Defective
      2  tesla  aaa  2003      5  Defective
      

      【讨论】:

        【解决方案3】:

        您可以将 np.whereisin 一起使用:

        df['Status'] = np.where(df['id'].isin(keys), 'Defective', 'OK')
        print(df)
        
        # Output
             car   id  year  wheel     Status
        0   audi   ab  2001      4         OK
        1  honda   aa  2002     15  Defective
        2  tesla  aaa  2003      5  Defective
        

        【讨论】:

          猜你喜欢
          • 2022-01-14
          • 1970-01-01
          • 2021-03-11
          • 2011-11-10
          • 1970-01-01
          • 1970-01-01
          • 2021-07-14
          • 2018-07-14
          • 1970-01-01
          相关资源
          最近更新 更多