Home > Net >  Sample a row from a subset of columns
Sample a row from a subset of columns

Time:04-28

Why is this throwing an error?

What is the real code I should type out?

I want to filter the dataframe by 2 column and then select a random row from that subset.

Code:

print(df_concepts.columns)
print(df_concepts[['id']['concept_code']].sample())

Traceback:

Index(['id', 'vocabulary_id', 'concept_code', 'concept_text'], dtype='object')
print(df_concepts[['id']['concept_code']].sample())
TypeError: list indices must be integers or slices, not str

Sorry if it is dead obvious

CodePudding user response:

Change nested lists first and then specify number of rows:

print(df_concepts[['id', 'concept_code']].sample(1))
  • Related