For a dataframe with label group
of some integers, what is the best way to get the sub-dataframe based on a list of group labels?
map = [[9,13],[21,22]]
train
group feature1
id
0 24 -0.141691
1 22 -0.527684
2 13 -0.586026
3 9 0.233221
4 21 -0.545011
Currently I am using for loop which obviously is sub-optimal.
df = pd.DataFrame()
for i in map[0]:
new_df = pd.concat([df,train[train.group==i]],axis=0)
new_df
group feature1
id
2 13 -0.586026
3 9 0.233221
I think you're looking for pd.Series.isin
?
df[df['group'].isin(np.concatenate(map))]
group feature1
id
1 22 -0.527684
2 13 -0.586026
3 9 0.233221
4 21 -0.545011
If you want just the records associated with map[0]
:
df[df['group'].isin(map[0])]
group feature1
id
2 13 -0.586026
3 9 0.233221
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.