【问题标题】:Uppercase Last Couple Indexes of String Elements in Pandas SeriesPandas 系列中字符串元素的大写最后一对索引
【发布时间】:2018-08-16 23:58:15
【问题描述】:

我有以下熊猫系列:

test_series = pd.Series(['canton, nc', 'leicester, nc', 'asheville, nc', 'candler, nc',
       'marshall, nc', 'waynesville, nc', 'fletcher, nc',
       'hendersonville, nc', 'old fort, nc', 'horse shoe, nc',
       'black mountain, nc', 'maggie valley, nc', 'burnsville, nc',
       'weaverville, nc', 'zirconia, nc', 'swannanoa, nc',
       'hot springs, nc', 'arden, nc', 'east flat rock, nc', 'marion, nc',
       'mars hill, nc', 'flat rock, nc', 'rutherfordton, nc', 'clyde, nc',
       'saluda, nc', 'alexander, nc', 'fairview, nc', 'mill spring, nc',
       'brevard, nc', 'mills river, nc', 'penrose, nc',
       'pisgah forest, nc', 'barnardsville, nc', 'etowah, nc',
       'travelers rest, sc', 'lake lure, nc', 'montreat, nc', 'dana, nc',
       'greenville, sc', 'flag pond, tn', 'laurel park, nc'])

我想将州首字母缩写词设为大写。这是我最好的猜测:test_series.str[-2:].upper() 但我得到一个属性错误。这样做最有效的是什么?

【问题讨论】:

    标签: string python-3.x pandas methods series


    【解决方案1】:

    为了获得更好的性能,请在数据集中使用列表理解和没有 NaN:

    test_series = pd.Series([i[:-2] + i[-2:].upper() for i in test_series])
    

    等价性检验:

    (test_series.str[:-2] + test_series.str[-2:].str.upper() == pd.Series([i[:-2] + i[-2:].upper() for i in test_series])).all()
    
     True
    

    时间安排:

    %timeit test_series.str[:-2] + test_series.str[-2:].str.upper()
    1000 loops, best of 3: 1.1 ms per loop
    
    %timeit pd.Series([i[:-2] + i[-2:].upper() for i in test_series])
    1000 loops, best of 3: 245 µs per loop
    

    输出:

    0             canton, NC
    1          leicester, NC
    2          asheville, NC
    3            candler, NC
    4           marshall, NC
    5        waynesville, NC
    6           fletcher, NC
    7     hendersonville, NC
    8           old fort, NC
    9         horse shoe, NC
    10    black mountain, NC
    11     maggie valley, NC
    12        burnsville, NC
    13       weaverville, NC
    14          zirconia, NC
    15         swannanoa, NC
    16       hot springs, NC
    17             arden, NC
    18    east flat rock, NC
    19            marion, NC
    20         mars hill, NC
    21         flat rock, NC
    22     rutherfordton, NC
    23             clyde, NC
    24            saluda, NC
    25         alexander, NC
    26          fairview, NC
    27       mill spring, NC
    28           brevard, NC
    29       mills river, NC
    30           penrose, NC
    31     pisgah forest, NC
    32     barnardsville, NC
    33            etowah, NC
    34    travelers rest, SC
    35         lake lure, NC
    36          montreat, NC
    37              dana, NC
    38        greenville, SC
    39         flag pond, TN
    40       laurel park, NC
    dtype: object
    

    【讨论】:

    • 是的,但最好使用 pandas .str 解决方案,因为如果至少有一个 NaN,纯 python 会失败。
    【解决方案2】:

    首先选择所有没有last 2的值,并添加到被str.upper转换为大写的last 2值:

    test_series = test_series.str[:-2] + test_series.str[-2:].str.upper()
    print (test_series.head())
    0       canton, NC
    1    leicester, NC
    2    asheville, NC
    3      candler, NC
    4     marshall, NC
    dtype: object
    

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 2016-07-14
      • 2020-02-19
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2013-01-11
      • 2013-05-06
      相关资源
      最近更新 更多