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

Time:12-07

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)

CodePudding user response:

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

CodePudding user response:

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)
  • Related