简体   繁体   中英

How to remove rows from DataFrame

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM