question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataFrame.loc does not work with list of indices

See original GitHub issue

If i = [3, 5, 1] and df is a pandas data frame this returns the numbered rows in the defined order: df.loc[i].

This is the expected behavior for dask.dataframe.DataFrame but if df is such an object then df.loc[i] does not work. Note that you can give it a slice: df.loc[3:6] for example.

On a side note, although using numbers in the example above, loc is for labels and as such we could expect partitions made of indices (or multindices) of strings, datetimes, and other objects…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abastcommented, Nov 7, 2016

What about this one for larger lists?

ddf.map_partitions(lambda x: x[x.index.isin(L)])

0reactions
jdthorpecommented, Aug 30, 2022

I’m a little late to the party, but as of now, this is is an effective solution:

ddf.loc[ddf.index.isin(L)].compute()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Select Pandas rows based on list index - python
Use .iloc for integer based indexing and .loc for label based indexing. ... This will not work if the indexes in your dataframe...
Read more >
pandas.DataFrame.loc — pandas 1.5.2 documentation
pandas.DataFrame.loc# ... Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also...
Read more >
Pandas Select Rows Based on List Index
You can select rows from a list of Index in pandas DataFrame either using DataFrame.iloc[], DataFrame.loc]. iloc[] takes row indexes as a list....
Read more >
A Python Beginner's Look at .loc. As ...
My first tip is that .loc is not a dataframe method; using () instead of ... You can also pass lists with row...
Read more >
Pandas iloc and loc – quickly select data in DataFrames
The iloc, loc and ix indexers for Python Pandas select rows and columns from DataFrames. Simple guide to find data by position, label...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found