Shamoon
Shamoon

Reputation: 43639

How can I sample from a subset of a pandas DataFrame?

How can I select a random row from a pandas DataFrame between the 0th row and the length - 100?

Something like start_state = self.market_data.sample(1), except that chooses from ANY row (even the last 100).

Upvotes: 1

Views: 110

Answers (1)

jezrael
jezrael

Reputation: 863611

You can use DataFrame.iloc for select by positions.

If need filter first 100 rows:

self.market_data.iloc[:100].sample(1)

Or DataFrame.head:

self.market_data.head(100).sample(1)

If want filter all data without last 100 rows:

self.market_data.iloc[:-100].sample(1)

Upvotes: 3

Related Questions