【问题标题】:Boolean indexing to retain falsy values as NaN布尔索引以将虚假值保留为 NaN
【发布时间】:2017-07-31 22:21:10
【问题描述】:

给定一个数据框:

                         Data
1                      246804
2                      135272
3                      898.01
4                     3453.33
5                       shine  
6                        add
7                         522
8                         Nan
9                      string
10                      29.11
11                        20  

我想要两个新列 FloatsStrings,它们的长度都与原始数据帧相同。获取Floats 列很容易:

In [176]: pd.to_numeric(df.Data, errors='coerce')
Out[176]: 
1     246804.00
2     135272.00
3        898.01
4       3453.33
5           NaN
6           NaN
7        522.00
8           NaN
9           NaN
10        29.11
11        20.00
Name: Data, dtype: float64

如您所见,非浮点数被强制转换为NaN,这正是我想要的。

要获取字符串,我是这样做的:

In [177]: df[df.Data.str.isalpha()]
Out[177]: 
     Data
5   shine
6     add
8     Nan
9  string

但正如您所见,它不会将非字符串值保留为NaN。我想要这样的东西:

1                       NaN
2                       NaN
3                       NaN
4                       NaN
5                       shine  
6                       add
7                       NaN
8                       Nan (not NaN)
9                       string
10                      NaN
11                      NaN  

我怎样才能让它这样做?

【问题讨论】:

    标签: python pandas dataframe nan


    【解决方案1】:

    要获取Strings,您可以在Data 列上使用布尔索引,并位于Floats 为空的位置。

    df['Floats'] = pd.to_numeric(df.Data, errors='coerce')
    df['Strings'] = df.Data.loc[df.Floats.isnull()]  # Optional: .astype(str)
    
    >>> df
    # Output:
    #        Data     Floats Strings
    # 1    246804  246804.00     NaN
    # 2    135272  135272.00     NaN
    # 3    898.01     898.01     NaN
    # 4   3453.33    3453.33     NaN
    # 5     shine        NaN   shine
    # 6       add        NaN     add
    # 7       522     522.00     NaN
    # 8       Nan        NaN     Nan
    # 9    string        NaN  string
    # 10    29.11      29.11     NaN
    # 11       20      20.00     NaN
    

    【讨论】:

    • 虽然其他答案都很棒,但老实说,我最欣赏它的简单性。谢谢!
    【解决方案2】:
    floats = pd.to_numeric(df.Data, 'coerce')
    pd.DataFrame(dict(
        floats=floats,
        strings=df.Data.mask(floats.notnull())
    ))
    
           floats strings
    1   246804.00     NaN
    2   135272.00     NaN
    3      898.01     NaN
    4     3453.33     NaN
    5         NaN   shine
    6         NaN     add
    7      522.00     NaN
    8         NaN     Nan
    9         NaN  string
    10      29.11     NaN
    11      20.00     NaN
    

    您甚至可以在 mask 中通过传递替代项来使其更加明显

    floats = pd.to_numeric(df.Data, 'coerce')
    pd.DataFrame(dict(
        floats=floats,
        strings=df.Data.mask(floats.notnull(), '')
    ))
    
           floats strings
    1   246804.00        
    2   135272.00        
    3      898.01        
    4     3453.33        
    5         NaN   shine
    6         NaN     add
    7      522.00        
    8         NaN     Nan
    9         NaN  string
    10      29.11        
    11      20.00        
    

    【讨论】:

    • 我也喜欢这个答案,因为它教会了我一些东西。我希望我能接受不止一个,但我不得不接受亚历山大的回答,因为这似乎是最简单的。尽管如此,还是谢谢你的回答:)
    • 不要让我对这篇文章进行所有的社论。在他发布答案半秒钟后,我对他的答案投了赞成票。比起接受的答案,我更关心赞成票。我认为开箱即用,我的答案通常看起来与其他人的不同。我希望很多时候别人的回答会更可口。只有当我看到 OP 在非常好的答案中根本没有选择答案时,我才会感到不满。另外,不要觉得你必须解释或证明你的选择。他的回答很好,我是成年人,我完全可以应付。嗯……也许不是成年人,但是 w/e。
    • @piRSquared 谢谢...我可以尊重这一点。以后我会有很多问题...请继续用您的专业知识为我们所有人增光添彩!
    【解决方案3】:

    怎么样

    df.Data.where(pd.to_numeric(df.Data, errors='coerce').isnull())
    Out[186]: 
          Data
    1      NaN
    2      NaN
    3      NaN
    4      NaN
    5    shine
    6      add
    7      NaN
    8      Nan #not NaN
    9   string
    10     NaN
    11     NaN
    

    或基于您的df.Data.str.isalpha()

    df['Data'].where(df['Data'].str.isalpha())
    

    【讨论】:

    • 我喜欢你的回答!
    • Brb,撤消一些反对票以支持您的答案 xD
    • 这让我笑了
    • Nitpick:用这个就是你修改原来的列。
    • 另外,这个答案会覆盖Data中的原始值,所以数字数据会丢失。
    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 2017-08-18
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2020-03-22
    相关资源
    最近更新 更多