Reputation: 315
I have 2 DataFrames:
dfA: dfB:
n1 n2
a - AA 01 - 0
b - BB 02 - 0
c - CC 03 - 0
d - DD 04 - 1
e - EE 05 - 2
f - FF 06 - 0
Number of rows in dfA and dfB is always equal.
Now, I'm deleting all rows with 0 values from dfB by:
dfB = dfB[dfB['n2']!=0]
(Most likely 0 rows appear only on top or (top and bottom) of DataFrames.)
How to go about deleting corresponding rows from dfA?
Upvotes: 2
Views: 82
Reputation: 1053
You can extract the value of the index using get_indexer_for, which returns a list of index locations (as opposed to index values)
dfB.index.get_indexer_for(dfB[dfB['n2'] != 0].index)
Then you can use those to drop the corresponding values in dfA as follows
dfA.drop(dfa.index[dfB.index.get_indexer_for(dfB[dfB['n2'] != 0].index)])
Make sure to reassign the df as this operation does not modify existing dataframe. It just makes a copy of it.
Upvotes: 2
Reputation: 294308
Use dfB
to make a boolean array. Then use it as a mask for both dataframes.
m = dfB.n2.eq(0).values
dfB = dfB[m]
dfA = dfA[m]
Debugging
Copy/Paste this
dfB = pd.DataFrame(dict(n2=[0, 0, 0, 1, 2, 0]), '01 02 03 04 05 06'.split())
m = dfB.n2.eq(0).values
dfB[m]
Upvotes: 2