Select Rows with loc and iloc

Build a DataFrame from records using labels as its unique string index. Return a dictionary with labelled: the rows requested by selected_labels, in that order; and positioned: the rows in the positional slice start:stop. Return each set as a list of row dictionaries without the index. Requested labels exist. Positional slicing follows Python rules: stop is excluded and negative positions are allowed.

Difficulty: beginner

Show hint

Set index=labels on the DataFrame. Use df.loc[selected_labels] for label selection and df.iloc[start:stop] for positional selection. Convert both results to record dictionaries.

Examples

  • [[{"name":"Asha"},{"name":"Ben"},{"name":"Mina"}],["c","a","b"],["b","c"],0,2]{"labelled":[{"name":"Mina"},{"name":"Asha"}],"positioned":[{"name":"Asha"},{"name":"Ben"}]}

Your solution

Python · Pandas

Results

Python and Pandas load when you Run or Submit. Return a Python list, dictionary or scalar as requested.

Back to Lesson