简体   繁体   中英

Extracting rows from original data frame based on condition to create new data frame

Given a data frame, I am interested in extracting rows with IDs that match IDs in a given list and turning these rows into a new data frame.

Example:

ids_list=[123, 345, 567, 789, 234, 456, 678]

oringal_df

ids grade major
123 98 Engineering
345 100 English
111 64 History
456 85 Drama
444 75 Math

new_df is created with rows where oringal_df[ids] is in ids_list

ids grade major
123 98 Engineering
345 100 English
456 85 Drama

I wrote the following and I have tried other variations but I keep getting errors. Please help.

for i in original_df.loc[original_df['ids']]:
    if original_df[i].isin(ids_list):
        data=original_df.loc[original_df[i]]
        new_df=pd.DataFrame(data)

This is better:

new_df = df.loc[df['ids'].isin(ids_list)]
print(new_df)

output:

   ids  grade        major
0  123     98  Engineering
1  345    100      English
3  456     85        Drama

using merge should do what you want:

ids_list=[123, 345, 567, 789, 234, 456, 678]
id_df = pd.DataFrame(ids_list , columns = ["sid"])
mergedf = pd.merge(original_df , id_df , on = 'sid)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM