Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

Change nested lists first and then specify number of rows:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading