Tilde登录python数据框 [英] Tilde sign in python dataframe

查看:42
本文介绍了Tilde登录python数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,遇到了一个代码段.

Im new to python and came across a code snippet.

df = df[~df['InvoiceNo'].str.contains('C')]

如果我能知道在此情况下代字号使用了什么,那将是非常有义务的吗?

Would be much obliged if I could know whats the tilde signs usage in this context ?

推荐答案

这意味着按位不对,将布尔掩码反转-将False s转换为True s,将True s转换为False s.

It means bitwise not, inversing boolean mask - Falses to Trues and Trues to Falses.

示例:

df = pd.DataFrame({'InvoiceNo': ['aaC','ff','lC'],
                   'a':[1,2,5]})
print (df)
  InvoiceNo  a
0       aaC  1
1        ff  2
2        lC  5

#check if column contains C
print (df['InvoiceNo'].str.contains('C'))
0     True
1    False
2     True
Name: InvoiceNo, dtype: bool

#inversing mask
print (~df['InvoiceNo'].str.contains('C'))
0    False
1     True
2    False
Name: InvoiceNo, dtype: bool

通过 boolean indexing 进行过滤:

df = df[~df['InvoiceNo'].str.contains('C')]
print (df)
  InvoiceNo  a
1        ff  2

所以输出是DataFrame的所有行,在InvoiceNo列中不包含C.

So output is all rows of DataFrame, which not contains C in column InvoiceNo.

这篇关于Tilde登录python数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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