Python[pandas]: Select certain rows by index of another dataframe
Use isin
:
df = df[df.index.isin(df1.index)]
Or get all intersectioned indices and select by loc
:
df = df.loc[df.index & df1.index]
df = df.loc[np.intersect1d(df.index, df1.index)]
df = df.loc[df.index.intersection(df1.index)]
print (df)
A B C D
1 1 4 9 1
3 5 5 1 0
22 1 3 9 6
EDIT:
I tried solution: df = df.loc[df1.index]. Do you think that this solution is correct?
Solution is incorrect:
df = df.loc[df1.index]
print (df)
A B C D
1 1.0 4.0 9.0 1.0
3 5.0 5.0 1.0 0.0
4 NaN NaN NaN NaN
5 NaN NaN NaN NaN
6 NaN NaN NaN NaN
7 NaN NaN NaN NaN
22 1.0 3.0 9.0 6.0
28 NaN NaN NaN NaN
29 NaN NaN NaN NaN
32 NaN NaN NaN NaN
C:/Dropbox/work-joy/so/_t/t.py:23: FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
print (df)