s_khan92
s_khan92

Reputation: 979

How to get list of columns from dataframe which start with specific value?

I have df which have different columns and some of them contains list and I want to get all the columns which contains list so I can run function only on them.

df:

Name      Test_Column1        Test_Column2           Test_Column3
A         ['a', 'b']           Test                   ['b']

So ideally i would like to get list of columns ['Test_Column1','Test_Column3']

Upvotes: 1

Views: 270

Answers (2)

jezrael
jezrael

Reputation: 863226

If want test all columns for lists use DataFrame.applymap:

cols = df.columns[df.applymap(lambda x: isinstance(x, list)).all()].tolist()
print (cols)
['Test_Column1','Test_Column3']

If want test only first row is possible simplify solution:

cols = df.columns[df.iloc[0].apply(lambda x: isinstance(x, list))].tolist()

Upvotes: 1

Alexis Drakopoulos
Alexis Drakopoulos

Reputation: 1145

This isn't clean but you could do something like:

pd.DataFrame({"listcol":[[1, 2], [2, 3]], "normalcol":[1, 2]})
for col in df:
    if all(df.loc[:,col].apply(lambda x: isinstance(x, list))):
        print("all values in", col, "are of type list!")

instead of printing you can then save them. This only works if all elements in the column are of type list. You could switch it out for any() or your own check. I'm sure theres a cleaner way of applying this across columns that doesn't require looping but it escapes me.

Upvotes: 3

Related Questions