python - 是否可以用 str.contain 定义句子中字符的索引?

标签 python pandas

我有这样的数据框

          UPC   CPC freq_moc all_pat
        0   101 B22F    1.0  |US8193442|0|
        1   101 B82Y    2.0  |0|US8193442|US8088309|0|
        2   106 B22F    3.0  |US4219448|0|0|0|0|0|US8372734|US8809678|0|0
        3   106 B82Y    12.0 |0|0|0|0|0|0|0|US5487792|0|0|US6538194|US68644

它是由这段代码生成的

df_pat = df_pat[df_pat.all_pat.str.contains("0")]
df_pat

但我想问是否可以打印出包含|0|的行在第一个索引处。预期的结果会像

        UPC CPC freq_moc all_pat
    1   101 B82Y    2.0  |0|US8193442|US8088309|0|
    3   106 B82Y    12.0 |0|0|0|0|0|0|0|US5487792|0|0|US6538194|US68644

最佳答案

您可以使用str.startswith,

df_pat[df_pat.all_pat.str.startswith("|0|")]

你得到

    UPC CPC    freq_moc all_pat
1   101 B82Y    2.0  |0|US8193442|US8088309|0|
3   106 B82Y    12.0 |0|0|0|0|0|0|0|US5487792|0|0|US6538194|US68644

关于python - 是否可以用 str.contain 定义句子中字符的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065759/

相关文章:

Python3 : Dynamically invoking a method with dictionary of parameters

python - 无法访问存储在 pandas 系列中的嵌套 JSON 字段

python - 如何将所选列的行值与另一个数据框中的相同列进行比较?

python - 访问 DataFrame 的每个条目并以更好的方式替换它们?

python - 对角线主教用 python 在棋盘上移动

python - 如何修复 "googlemaps.exceptions.ApiError : REQUEST_DENIED"

python - 如何使用 xlsWriterr 逐行获取输出

python - Pandas 分组行值调整

python - pandas 从日期时间转换为整数时间戳

python - 为不透明度和音量编写关键帧插入脚本