Skip to content Skip to sidebar Skip to footer

Pandas Dataframe Get Row Numbers And Add To List

Lets assume we have a panda dataframes with three features as represented below. Each rows is representing a customer and each column representing some features of this customer.

Solution 1:

How about like this?

ls = []

ls.extend(df.index[(df['feature1'] < 100 )])
ls.extend(df.index[(df['feature2'] > 500 )])

print(ls)
[4, 0, 1, 3]

Solution 2:

Not real clear... but maybe this:

df.query('feature1 < 100 | feature2 > 500').index.tolist()

[0, 1, 3, 4]

Solution 3:

You want to output the index as a list.

print(df[df['feature2'] > 500].index.tolist())

[0, 1, 3]

Post a Comment for "Pandas Dataframe Get Row Numbers And Add To List"