python长整型取消了_python – 如何删除整数类型的列中的最后两...

使用astype将dtype转换为str,然后使用vectorised str方法对str进行切片,然后再次转换回int64 dtype:

In [184]:

df['DATE'] = df['DATE'].astype(str).str[:-2].astype(np.int64)

df

Out[184]:

DATE

0 201107

1 201107

2 201107

3 201107

4 201107

5 201107

6 201107

7 201108

8 201108

9 201108

In [185]:

df.info()

Int64Index: 10 entries, 0 to 9

Data columns (total 1 columns):

DATE 10 non-null int64

dtypes: int64(1)

memory usage: 160.0 bytes

嗯…

原来有一个内置的方法floordiv:

In [191]:

df['DATE'].floordiv(100)

Out[191]:

0 201107

1 201107

2 201107

3 201107

4 201107

5 201107

6 201107

7 201108

8 201108

9 201108

Name: DATE, dtype: int64

更新

对于1000行df,floordiv方法要快得多:

%timeit df['DATE'].astype(str).str[:-2].astype(np.int64)

%timeit df['DATE'].floordiv(100)

100 loops, best of 3: 2.92 ms per loop

1000 loops, best of 3: 203 ?s per loop

在这里我们观察到~10倍的加速

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值