Python.从 Pandas 列中提取字符串的最后一个字母 [英] Python. Extract last letter of a string from a Pandas column

查看:644
本文介绍了Python.从 Pandas 列中提取字符串的最后一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将UserId"中的最后一位数字存储在一个新变量中(例如 UserId 是字符串类型).

I want to store in a new variable the last digit from a 'UserId' (such UserId is of type string).

我想出了这个,但它是一个很长的 df 并且需要永远.关于如何优化/避免 for 循环的任何提示?

I came up with this, but it's a long df and takes forever. Any tips on how to optimize/avoid for loop?

df['LastDigit'] = np.nan
for i in range(0,len(df['UserId'])):
    df.loc[i]['LastDigit'] = df.loc[i]['UserId'].strip()[-1]

推荐答案

使用 str.strip 使用 str[-1] 索引:

df['LastDigit'] = df['UserId'].str.strip().str[-1]

如果性能很重要并且没有缺失值,请使用列表理解:

If performance is important and no missing values use list comprehension:

df['LastDigit'] = [x.strip()[-1] for x in df['UserId']]

你的解决方案真的很慢,这是这个的最后一个解决方案:

Your solution is really slow, it is last solution from this:

6) 更新一个空帧(例如使用 loc one-row-at-a-time)

6) updating an empty frame (e.g. using loc one-row-at-a-time)

性能:

np.random.seed(456)
users = ['joe','jan ','ben','rick ','clare','mary','tom']
df = pd.DataFrame({
         'UserId': np.random.choice(users, size=1000),

})

In [139]: %%timeit
     ...: df['LastDigit'] = np.nan
     ...: for i in range(0,len(df['UserId'])):
     ...:     df.loc[i]['LastDigit'] = df.loc[i]['UserId'].strip()[-1]
     ...: 
__main__:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
57.9 s ± 1.48 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [140]: %timeit df['LastDigit'] = df['UserId'].str.strip().str[-1]
1.38 ms ± 150 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [141]: %timeit df['LastDigit'] = [x.strip()[-1] for x in df['UserId']]
343 µs ± 8.31 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

这篇关于Python.从 Pandas 列中提取字符串的最后一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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