View the step-by-step solution to:

I have a dataframe with many columns, but I just want to access a few of those columns (the ones in the list below).

I have a dataframe with many columns, but I just want to access a few of those columns (the ones in the list below). For each column in the list, I would like to extract the 'id' key of each row and put it in a new list called s. This is Python. How is this possible? Below is my attempt.


list_cols

[

  'genres',

  'belongs_to_collection',

  'production_companies',

  'production_countries',

  'spoken_languages',

  'Keywords',

  'cast',

  'crew'

]



for l in list_cols:

  if l in data.columns:

    s=[]

    for l in data.columns:

      m=[d['id'] for d in l if 'id' in d]

      s.append(m)     

      print(s)

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question