python drop row如果仅用于特定字符串

时间:2019-02-26 19:16:47

标签: python python-3.x pandas

当C列的独立数据值设置为“,”时,我试图删除行。下面的代码将删除所有包含','的行,如何删除value =',',而不删除其他任何内容?

df2 = pd.DataFrame(dict(A=[1,2,3,4,5,6], C=["Doe, John",",","Driver,A","Matt,", ",",","]))
df2
df2[~df2.C.str.contains(",")] # removes everything

输入:

A   C
1   Doe, John
2   ,
3   Driver,A
4   Matt,
5   ,
6   ,

所需的输出:

A   C
1   Doe, John
3   Driver,A
4   Matt,

1 个答案:

答案 0 :(得分:1)

鉴于您不需要确切的字符',可以这样做:

    df2[df2.C != ',']