I have two dataframes, that look like this
df1 =
name color
0 John Blue
1 John Red
2 Lucy Green
3 Lucy Blue
4 Max Blue
2 Max White
And
df2 =
name value
0 John 15
1 Lucy 20
2 Max 5
I am trying to drop all the grouped names in df1
whose value
in df2
is below 10 (in this case, I would want to drop all the rows with df1['name']='Max'
).
The result I am trying to get is:
df1 =
name color
0 John Blue
1 John Red
2 Lucy Green
3 Lucy Blue
Thanks!
Like this:
In [731]: res = pd.merge(df1, df2, on='name')
In [736]: res[res['value'].ge(10)][['name','color']]
Out[736]:
name color
0 John Blue
1 John Red
2 Lucy Green
3 Lucy Blue
#get names that are greater than or equal to 10
filtr = df2.loc[df2.value.ge(10),'name']
#extract names that match filtr
df1.loc[df1.name.isin(filtr)]
name color
0 John Blue
1 John Red
2 Lucy Green
3 Lucy Blue
df1['name'].isin(['Max']) # select the df1 which name is 'Max'
df1=df1[~df1['name'].isin(['Max'])] # reverse select the other elements.
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.