Python3, column names from array - numpy or pandas

Miki Tebeka miki.tebeka at gmail.com
Thu Dec 15 00:04:41 EST 2016


You can do this with pandas:

    import pandas as pd
    from io import StringIO

    io = StringIO('''\
    id        A        B        C        D        E 
    100        1        0        0        0        0 
    101        0        1        1        0        0 
    102        1        0        0        0        0 
    103        0        0        0        1        1 
    ''')

    df = pd.read_csv(io, sep='\s+', index_col='id')
    val = df.apply(lambda row: ' '.join(df.columns[row==1]), axis=1)

Google for "boolean indexing" to see what's going on :)


More information about the Python-list mailing list