如何修改一列中的多个值,但在 Pandas python 中跳过其他值 [英] How to modify multiple values in one column, but skip others in pandas python

查看:66
本文介绍了如何修改一列中的多个值,但在 Pandas python 中跳过其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 中进行了两个月,我现在正专注于 Pandas.在我目前的职位上,我在数据框上使用 VBA,所以学习这个以慢慢取代它并进一步发展我的职业生涯.到目前为止,我认为我真正的问题是缺乏对关键概念的理解.任何帮助将不胜感激.

Going on two months in python and I am focusing hard on Pandas right now. In my current position I use VBA on data frames, so learning this to slowly replace it and further my career. As of now I believe my true problem is the lack of understanding a key concept(s). Any help would be greatly appreciated.

这就是我的问题:

我可以去哪里了解有关如何执行此类操作以进行更精确过滤的更多信息.我非常接近,但我需要一个关键方面.

Where could I go to learn more on how to do stuff like this for more precise filtering. I'm very close but there is one key aspect I need.

主要目标我需要跳过 ID 列中的某些值.下面的代码取出破折号-";并且最多只能读取 9 位数字.但是,我需要跳过某些 ID,因为它们是唯一的.

Main goal I need to skip certain values in my ID column. The below code takes out the Dashes "-" and only reads up to 9 digits. Yet, I need to skip certain IDs because they are unique.

之后我将开始比较多张工作表.

  • 主数据框 ID 的格式为 000-000-000-000
  • 我将比较的其他数据框没有破折号-"如 000000000 和三个 000 共九位数字.

我需要跳过的唯一 ID 在两个数据帧中是相同的,但格式完全不同,范围从 000-000-000_#12、000-000-000_35 或 000-000-000_z.

The unique IDs that I need skipped are the same in both data frames, but are formatted completely different ranging from 000-000-000_#12, 000-000-000_35, or 000-000-000_z.

我将在除唯一 ID 之外的每个 ID 上使用的代码:

My code that I will use on each ID except the unique ones:

 dfSS["ID"] = dfSS["ID"].str.replace("-", "").str[:9]

但我想使用像这样的 if 语句(这不起作用)

but I want to use an if statement like (This does not work)

lst = ["000-000-000_#69B", "000-000-000_a", "etc.. random IDs", ]

if ~dfSS["ID"].isin(lst ).any()
    dfSS["ID"] = dfSS["ID"].str.replace("-", "").str[:9]
else:
    pass

为了进一步说明,我的输入 DataFrame 是这样的:

            ID               Street #   Street Name 
0   004-330-002-000         2272        Narnia  
1   021-521-410-000_128     2311        Narnia  
2   001-243-313-000         2235        Narnia  
3   002-730-032-000         2149        Narnia
4   000-000-000_a           1234        Narnia

我希望将其作为输出:

            ID               Street #   Street Name 
0   004330002               2272        Narnia  
1   021-521-410-000_128     2311        Narnia  
2   001243313000            2235        Narnia  
3   002730032000            2149        Narnia
4   000-000-000_a           1234        Narnia

注意事项:

  • dfSS 是我的 Dataframe 变量名称,也就是我正在使用的 excel.身份证"是我的专栏标题.事后将使其成为索引
  • 我在这项工作中的数据框很小,(行、列)的数量为 (2500, 125)
  • 我没有收到错误消息,所以我猜我可能需要某种循环.也开始用这个来测试循环.没有运气......还没有.
  • 这里是我一直在研究这个的地方:

    Here is where I have been to research this:

    推荐答案

    有很多方法可以做到这一点.这里的第一种方法不涉及编写函数.

    There are a number of ways to do this. The first way here doesn't involve writing a function.

    # Create a placeholder column with all transformed IDs
    dfSS["ID_trans"] = dfSS["ID"].str.replace("-", "").str[:9]
    dfSS.loc[~dfSS["ID"].isin(lst), "ID"] = dfSS.loc[~dfSS["ID"].isin(lst), "ID_trans"] # conditional indexing
    

    第二种方法是写一个有条件转换ID的函数,没有第一种方法快.

    The second way is to write a function that conditionally converts the IDs, and it's not as fast as the first method.

    def transform_ID(ID_val):
        if ID_val not in lst:
            return ID_val.replace("-", "")[:9]
    
    dfSS['ID_trans'] = dfSS['ID'].apply(transform_ID)
    

    这篇关于如何修改一列中的多个值,但在 Pandas python 中跳过其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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