我正在努力获取一些数据,当我在列表中获取这些数据时,我想将其写入csv文件(可能使用熊猫)。
我想要转换的数据以列表的形式表示:
['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']]]我想知道我能不能把它写成这样:
Name Tag Role Company Start End and so on...教育和体验的细节在不同的列表中各不相同,我尝试使用带有列属性的熊猫,但是失败了。我试着用每一行都有一个经验/教育细节的形式来做。
发布于 2021-06-13 19:03:57
您可以使用以下方法:
# 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)输出:单击此处
发布于 2021-06-13 17:55:55
我假设您正在对某个数据库进行迭代,在每次迭代中,您将得到上面提到的嵌套列表。
在这里,对于“Val Guene”这个人来说,总共有9份工作和3份“大学”,因此,如果你连续拥有单身的“经验”和“大学”,那就没有意义了。(至于你会选择哪所大学的“高级助理”。)您可以做的是使用其中之一来创建一个数据文件。
所以让我们用“经验”
让我们的嵌套列表用变量list1来表示,
list1[0] :-“人名”
list1[1] :-“标签/当前职务”
list1[2] :-“经验”
list1[3] :-“大学”
哪里,
t=pd.DataFrame(list1[2])
t['name'] = list1[0]
t['role'] = list1[1]
t将为您提供所需的数据:

我想这就是你想要的。
https://stackoverflow.com/questions/67960438
复制相似问题