加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

显示不以“.0”Python Pandas结尾的值

发布时间:2020-12-20 11:54:42 所属栏目:Python 来源:网络整理
导读:我有一个包含NaN值和浮点值的浮点列.如何过滤掉那些不以.0结尾的值? 例如: Col10.71.01.19.09.5NaN 欲望结果将是: Col10.71.1 9.2 解决方法 你可以使用 boolean indexing : #convert to string and compare last valueprint ((df.Col1.astype(str).str[-
我有一个包含NaN值和浮点值的浮点列.如何过滤掉那些不以.0结尾的值?

例如:

Col1
0.7
1.0
1.1
9.0
9.5
NaN

欲望结果将是:

Col1
0.7
1.1 
9.2

解决方法

你可以使用 boolean indexing

#convert to string and compare last value
print ((df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull()))
0     True
1    False
2     True
3    False
4     True
5    False
Name: Col1,dtype: bool

print (df[(df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull())])
   Col1
0   0.7
2   1.1
4   9.5

将转换后的值与“ˉnt”进行比较的另一种解决方案,但首先需要fillna

s = df.Col1.fillna(1)
print (df[s.astype(int) != s])
   Col1
0   0.7
2   1.1
4   9.5

时序:

#[30000 rows x 1 columns]
df = pd.concat([df]*10000).reset_index(drop=True)

def jez2(df):
    s = df.Col1.fillna(1)
    return (df[s.astype(int) != s])

In [179]: %timeit (df[(df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull())])
10 loops,best of 3: 80.2 ms per loop

In [180]: %timeit (jez2(df))
1000 loops,best of 3: 1.16 ms per loop

In [181]: %timeit (df[df.Col1 // 1 != df.Col1].dropna())
100 loops,best of 3: 3.04 ms per loop

In [182]: %timeit (df[df['Col1'].mod(1) > 0].dropna())
100 loops,best of 3: 2.58 ms per loop

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读