如何删除" 1"后跟数据框行值的尾随零?

时间:2014-05-13 05:09:45

标签: pandas dataframe

enter image description here

从我的“Id”栏我想要从左边删除一个和零。 那是 1000003变为3 1000005变为5 1000011变为11,依此类推

忽略-1,10和1000000,它们将作为特殊情况处理。但是从剩下的行我想删除“1”后跟零。

3 个答案:

答案 0 :(得分:1)

你可以使用模数来得到数字的结尾(它们将是余数)。因此,只需使用[-1,10,1000000]的ID排除行,然后计算模数1000000:

print df

        Id
0       -1
1       10
2  1000000
3  1000003
4  1000005
5  1000007
6  1000009
7  1000011

keep = df.Id.isin([-1,10,1000000])
df.Id[~keep] = df.Id[~keep] % 1000000
print df

        Id
0       -1
1       10
2  1000000
3        3
4        5
5        7
6        9
7       11

编辑:这是一个完全矢量化的字符串切片版本作为替代方案(与Alex'方法一样,但利用了pandas'矢量化字符串方法):

keep = df.Id.isin([-1,10,1000000])
df.Id[~keep] = df.Id[~keep].astype(str).str[1:].astype(int)
print df

        Id
0       -1
1       10
2  1000000
3        3
4        5
5        7
6        9
7       11

答案 1 :(得分:1)

这是你可以尝试的另一种方式:

def f(x):
    """convert the value to a string, then select only the characters
       after the first one in the string, which is 1. For example,
       100005 would be 00005 and I believe it's returning 00005.0 from 
       dataframe, which is why the float() is there. Then just convert 
       it to an int, and you'll have 5, etc.
    """
    return int(float(str(x)[1:]))

# apply the function "f" to the dataframe and pass in the column 'Id'
df.apply(lambda row: f(row['Id']), axis=1)

答案 2 :(得分:1)

我知道这个问题很满意。但对于未来的访客,我喜欢亚历克斯'答案是它不依赖于那里恰好有四个零。如果您有时10005,有时1000005等等,则接受的答案将失败。

但是,要添加更多内容以便我们考虑它。如果你知道它总是10000,你可以做

# backup all values
foo = df.id 
#now, some will be negative or zero
df.id = df.id - 10000 
#back in those that are negative or zero (here, first three rows)
df.if[df.if <= 0] = foo[df.id <= 0] 

它给你的答案与卡尔的答案相同,但我通常更喜欢这些方法的可读性。