Home>

Data
a
a b
a b b
a b c
a b c d
a a a b
a d d d a

Suppose you have a csv with the structure

.
At this time, I want to delete the line containing b. At this time,
a
a d d d a
I want to return.
df.drop ('b', axis = 0) did not work.

  • Answer # 1

    Question data is

    "a, b, c, d are stored in separate columns" (7x5 DF)

    "a, b, c, d are stored in the same column as part of the string" (7x1 DF)

    I can't tell which one is, so there are two answers

    When "a, b, c, d are stored in separate columns"

    import pandas as pd
    import io
    data = "" "
    A, B, C, D, E
    a ,,,,
    a, b ,,,
    a, b, b ,,
    a, b, c ,,
    a, b, c, d,
    a, a, a, b,
    a, d, d, d, a
    "" "
    df = pd.read_csv (io.StringIO (data)) #, header = None)
    ret = df [~ (df == 'b'). any (axis = 1)]
    print (ret)
    # A B C D E
    # 0 a NaN NaN NaN NaN
    # 6 a d d d a

    "a, b, c, d are stored in the same column as part of the string"

    data = "" "
    A
    a
    a b
    a b b
    a b c
    a b c d
    a a a b
    a d d d a
    "" "
    df = pd.read_csv (io.StringIO (data))
    ret = df [~ df.iloc [:, 0] .str.contains ('b')]
    print (ret)
    # A
    # 0 a
    # 6 a d d d a

    In any case,

    Create Boolian list of whether or not'b'is included in the line

    Filter the original data frame using that list

    I think it can be achieved