2

I am reading this frame from file:

 df = pd.read_csv('playlist.csv')

and fetching all values under column 'track':

 tracks = df['track'].tolist()

then I have a new_df with the same order of columns as 'playlist.csv':

new_df = pd.DataFrame(columns=['artist','track', 'value'])

and, given other_tracks = ['Once', 'Jeremy'], I would to add the entire row of some tracks to the new_df

for track in tracks:
    if track in other_tracks:
         row = df[df['track']==track]

how do I do it?

is there a simple way of doing this?

1 Answer 1

3

IIUC you don't need to loop, you can simply use boolean indexing:

new_df = df.loc[df['track'].isin(other_tracks)]
Sign up to request clarification or add additional context in comments.

3 Comments

One thing to note here is that tracks should contain the filtered tracks only.
I've edited my question, because I need to use the condition. could you please contemplate it in your answer?
@outkast, i've edited my answer... Please try to provide sample and expected data sets when asking Pandas/Numpy/SKLearn/etc. - related questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.