Search code examples
pandasdataframecolumn-selection

Selective column with data from a large pandas dataframe


I have a large dataframe with 1000 columns. I need to pick columns where they do not start with a particular string and assign it to another dataframe. How can I do this?

Something like below:

prefixes = ['fit test and human sorry',"perspectum liver scanaccurate","the following tests were not","apo b to  a ratio.this is a useful","psa"]
df_new = [ele for ele in prefixes if(ele.lower() not in df.columns)][0]

Solution

  • Here is a simpler version:

    import pandas as pd
    
    df = pd.DataFrame({'steve':[1,2,3],'bob':[1,2,3],'rob':[1,2,3]})
    prefixes = ('s', 'r')
    df[df.columns[~df.columns.str.startswith(prefixes)]]
    
       bob
    0    1
    1    2
    2    3