Hackerds
Hackerds

Reputation: 1205

Pandas: For each Unique ID, iterate over a list of strings and print it

I have a dataframe like the below and I am trying to print the list of subjects for each unique ID.

 ID  Name    Subjects
  0  Tom    [maths,chem,history....]
  1  Harry  [biology,physics,maths...]

And then iterate over the length of the subject list to do different operations.

    for Subjects in ID:   
        print(Subjects)
    for idx in range(len(Subjects)):
        -- Do operations ---

I did this:

for df.Subjects in df.ID:
      print(df.Subjects) 

But this printed ID Numbers.

Upvotes: 0

Views: 44

Answers (1)

Ashwini
Ashwini

Reputation: 393

Try this.

for sub in df.ID:
  print(df.loc[sub]['Subjects'])

Upvotes: 2

Related Questions