gpt4 book ai didi

python - 波浪号登录 Pandas 数据框

转载 作者:IT老高 更新时间:2023-10-28 20:55:26 27 4
gpt4 key购买 nike

我是 python/pandas 的新手,遇到了一个代码片段。

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

如果我能知道波浪号在这种情况下的用法,我会非常感激吗?

最佳答案

这意味着按位不,反转 bool 掩码 - Falses 到 Trues 和 Trues 到 False秒。

示例:

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

关于python - 波浪号登录 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com