How to deal with nested lists to csv in python

Viewed 212

I am working on getting some data, and as I am getting this data in a list, I want to write it into a csv file (maybe using pandas)

The data I want to convert is in the form of list:

['Val Guene',
 'Vice President at Global Impact Partners',
 [['Vice President',
   'Global Impact Partners',
   'Apr 2019',
   'Present',
   '2 yrs 3 mos',
   'N/A',
   ' '],
  ['Executive Board Member',
   'Prismflare',
   'Nov 2018',
   'Present',
   '2 yrs 8 mos',
   'N/A',
   ''],
  ['Co-Founder',
   'Prismflare',
   'Jul 2017',
   'Nov 2018',
   '1 yr 5 mos',
   'N/A',
   ''],
  ['Executive Board Member',
   'SDS Masala',
   'Feb 2019',
   'Apr 2021',
   '2 yrs 3 mos',
   'New Delhi Area, India',
   ' '],
  ['Manager',
   'PwC',
   'Jul 2018',
   'Jan 2019',
   '7 mos',
   'Greater New York City Area',
   ''],
  ['Senior Associate', 'PwC', 'Jul 2015', 'Jun 2018', '3 yrs', 'N/A', ''],
  ['Experienced Associate', 'PwC', 'Jul 2013', 'Jun 2015', '2 yrs', 'N/A', ''],
  ['Associate', 'PwC', 'Aug 2012', 'Jun 2013', '11 mos', 'N/A', ''],
  ['Fellow',
   'Martindale Center for the Study of Private Enterprise',
   'Jan 2011',
   'Aug 2012',
   '1 yr 8 mos',
   'N/A',
   ' ']],
 [['Harvard University', 'Graduate', 'Philosophy', '2012', '2012'],
  ['Lehigh University',
   "Bachelor's degree",
   'Economics, International Relations, Psychology',
   '2008',
   '2012'],
  ['UWC-USA', 'International Baccalaureate', 'Economics', '2006', '2008']]]

I am wondering if I could get it in the form:

Name          Tag           Role            Company         Start        End and so on...

The education and experience detail varies in different lists, I tried using pandas with columns attribute but it failed. I am trying to make it in the form where each row has one experience/education detail

2 Answers

I assume you are iterating over some database where in each iteration you are getting the nested list you have mentioned above.

Here you have, for the person 'Val Guene', total 9 jobs and 3 'University' so, for having both single 'experience' and single 'University' in a row, it wouldn't make sense.( as for like 'Senior Associate' which 'University' you will choose.) what you can do is use one of these to create a dataframe.

So let's use 'Experience'

let our this nested list be denoted by variable list1 then,

list1[0] :- 'name of person'

list1[1] :- 'tag/current job'

list1[2] :- 'Experience'

list1[3] :- 'University'

where,

t=pd.DataFrame(list1[2])
t['name'] = list1[0]
t['role'] = list1[1]
t

will give you required dataframe:

screenshot of output

I guess this is what you have asked for.

You can use the following approach:

  1. Create a dataframe with information about experience.
  2. Create a dataframe with information about education.
  3. Concat both dataframes.
  4. Create your CSV.
# We suppose the indexes will be the sames in order to get always the correct data.

experience_data = data[2]
education_data = data[3]
name = data[0]
tag = data[1]

df_experience = pd.DataFrame(experience_data, columns=['Rol', 'Company', 'Start', 'End', 'Duration', 'City', 'Achieves (?)'])
df_experience['Name'] = name
df_experience['Tag'] = tag
df_experience = df_experience[['Name', 'Tag', 'Rol', 'Company', 'Start', 'End', 'Duration', 'City', 'Achieves (?)']]

df_education = pd.DataFrame(education_data, columns=['University', 'Degree', 'Field', 'Start Education', 'End Education'])
df = pd.concat([df_experience, df_education], axis=1)
df.to_csv('your/path/file.csv', index=False)

Output: click here

Related