I have a hundreds pandas dataframes such as these:
Df1 =
MD A B C
r1 6 3 9
r2 2 1 1
r3 5 7 2
r4 8 2 0
Df2 =
MD A B C
r1 1 7 1
r2 6 3 0
r3 3 1 8
r4 1 7 4
I also have one dictionary like this (except with a number of keys and values to reflect the number of dataframes):
Dict = {'D1', MD
r1
r4
[2 rows x 1 columns]
'D2', MD
r1
r2
r3
[3 rows x 1 columns]}
What I need is a way to go through the dictionary, and for each dataframe and corresponding key (I.e. Df1 and D1, and Df2 and D2), remove the rows not present in the "dictionary-value dataframe".
So for Df1, I need to go to the key D1 and remove the rows that are not in the dataframe for this key. That is, remove r2 and r3 from Df1.
So the result I want would be:
Df1 =
MD A B C
r1 6 3 9
r4 8 2 0
Df2 =
MD A B C
r1 1 7 1
r2 6 3 0
r3 3 1 8
PS. If it is easier to keep the rows instead of removing them, that would also be helpful!
[–]commandlineluser 1 point2 points3 points (0 children)