'Sample a row from a subset of columns

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



Solution 1:[1]

Change nested lists first and then specify number of rows:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1