从Python数据框的一列中的每一行中删除前x个字符 [英] Remove first x number of characters from each row in a column of a Python dataframe

查看:362
本文介绍了从Python数据框的一列中的每一行中删除前x个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约1,500行15列的Python数据框.对于一个特定的列,我想删除每行的前3个字符.作为一个简单的示例,下面是一个数据框:

I have a Python dataframe with about 1,500 rows and 15 columns. With one specific column I would like to remove the first 3 characters of each row. As a simple example here is a dataframe:

import pandas as pd

d = {
    'Report Number':['8761234567', '8679876543','8994434555'],
    'Name'         :['George', 'Bill', 'Sally']
     }

d = pd.DataFrame(d)

我想从数据框dReport Number列的每个字段中删除前三个字符.

I would like to remove the first three characters from each field in the Report Number column of dataframe d.

推荐答案

使用矢量化的 str 方法来切片每个字符串条目

Use vectorised str methods to slice each string entry

In [11]:
d['Report Number'] = d['Report Number'].str[3:]
d

Out[11]:
     Name Report Number
0  George       1234567
1    Bill       9876543
2   Sally       4434555

这篇关于从Python数据框的一列中的每一行中删除前x个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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