-1

I have a DataFrame with n rows and an ndarray with n values (-1 for outliers and 1 for inlier). Is there a pythonic way to remove DataFrame rows that match the indices of the elements of the nparray marked as -1?

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Zito Relova Nov 07 '18 at 23:17
  • 1
    Possible duplicate of [Select rows from a DataFrame based on values in a column in pandas](https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas) – Brad Solomon Nov 07 '18 at 23:18

1 Answers1

0

You can just do: new_df = old_df[arr == 1].

Example:

df = pd.DataFrame(np.random.randn(5,5))

arr = np.random.choice([1,-1], 5)
>>> df
          0         1         2         3         4
0 -0.238418  0.291475  0.139162 -0.030003 -0.515817
1 -0.162404 -1.272317  0.342051 -0.787938  0.464699
2 -0.965481  0.727143 -0.887149 -0.430592 -2.074865
3  0.699129 -0.242738  1.754805 -0.120637 -1.536973
4  0.228538  0.799445 -0.217787  0.398572 -1.255639
>>> arr
array([ 1, -1, -1,  1, -1])

>>> df[arr == 1]
          0         1         2         3         4
0 -0.238418  0.291475  0.139162 -0.030003 -0.515817
3  0.699129 -0.242738  1.754805 -0.120637 -1.536973
sacuL
  • 49,704
  • 8
  • 81
  • 106