首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pd.read_csv()导入列表

pd.read_csv()导入列表
EN

Stack Overflow用户
提问于 2021-11-22 09:59:26
回答 2查看 83关注 0票数 1

目标:导入一个列.csv作为扁平列表。

ESG_BENEFITS.csv

代码语言:javascript
复制
import pandas pd

BENEFITS = pd.read_csv('../data/gri/ESG/ESG_BENEFITS.csv').values.tolist()

print(BENEFITS)

输出:

代码语言:javascript
复制
[['Accident insurance'], ['Adoption or fertility assistance programs'], ['Disability/invalidity insurance'], ['Mortgages and loans'], ['Pension plans/retirement provision'], ['Maternity and/or paternity leave'], ['Child care'], ['Job security initiatives for redeployment, including retraining, relocation, work-sharing and outplacement services'], ['Flexible workschemes and work-sharing'], ['Recall rights for laid-off employees'], ['Stock ownership'], ['Vacation'], ['Paid sick days'], ['PTO (including any of the following: unspecified, vacation and/or sick days)'], ['Insurance: Healthcare Employee'], ['Insurance: Healthcare Family'], ['Insurance: Healthcare Domestic Partner'], ['Insurance: Dental'], ['Insurance: Vision'], ['Insurance: AD&D'], ['Insurance: Short Term Disability'], ['Insurance: Long Term Disability'], ['Employee Assistance Program'], ['Education Benefits: Employee'], ['Education Benefits: Family'], ['Sabbatical Program'], ['Relocation Assistance'], ['Work/Life Support Program'], ['Wellness/Fitness Program'], ['Onsite Fitness Facilities'], ['Onsite Recreation Facilities'], ['Stock Options'], ['Stock Purchase Plan'], ['Employee Profit Sharing'], ['Retirement: Defined Benefit Plan (including pension plans)'], ['Childcare: Other'], ['Bereavement Leave'], ['Tuition reimbursement (other than career training)'], ['Gym facilities or gym fee reimbursement programs'], ['Higher education scholarship programs, for either employees or their relatives'], ['Preventative healthcare programs'], ['Flex scheduling'], ['Telecommuting options'], ['Public transportation subsidy'], ['Carpooling support programs'], ['Employee recognition programs'], ['Paid time off for employee volunteers'], ['Workforce training, skills, and leadership development programs'], ['Matching gift program'], ['Mentoring Program'], ['Others'], ['No additional benefits offered']]

想要的输出:扁平列表,忽略额外的间距。

代码语言:javascript
复制
['Accident insurance'  ,   'Adoption or fertility assistance programs'  ,   'Disability/invalidity insurance'  ,   'Mortgages and loans'  ,   'Pension plans/retirement provision'  ,   'Maternity and/or paternity leave'  ,   'Child care'  ,   'Job security initiatives for redeployment, including retraining, relocation, work-sharing and outplacement services'  ,   'Flexible workschemes and work-sharing'  ,   'Recall rights for laid-off employees'  ,   'Stock ownership'  ,   'Vacation'  ,   'Paid sick days'  ,   'PTO (including any of the following: unspecified, vacation and/or sick days)'  ,   'Insurance: Healthcare Employee'  ,   'Insurance: Healthcare Family'  ,   'Insurance: Healthcare Domestic Partner'  ,   'Insurance: Dental'  ,   'Insurance: Vision'  ,   'Insurance: AD&D'  ,   'Insurance: Short Term Disability'  ,   'Insurance: Long Term Disability'  ,   'Employee Assistance Program'  ,   'Education Benefits: Employee'  ,   'Education Benefits: Family'  ,   'Sabbatical Program'  ,   'Relocation Assistance'  ,   'Work/Life Support Program'  ,   'Wellness/Fitness Program'  ,   'Onsite Fitness Facilities'  ,   'Onsite Recreation Facilities'  ,   'Stock Options'  ,   'Stock Purchase Plan'  ,   'Employee Profit Sharing'  ,   'Retirement: Defined Benefit Plan (including pension plans)'  ,   'Childcare: Other'  ,   'Bereavement Leave'  ,   'Tuition reimbursement (other than career training)'  ,   'Gym facilities or gym fee reimbursement programs'  ,   'Higher education scholarship programs, for either employees or their relatives'  ,   'Preventative healthcare programs'  ,   'Flex scheduling'  ,   'Telecommuting options'  ,   'Public transportation subsidy'  ,   'Carpooling support programs'  ,   'Employee recognition programs'  ,   'Paid time off for employee volunteers'  ,   'Workforce training, skills, and leadership development programs'  ,   'Matching gift program'  ,   'Mentoring Program'  ,   'Others'  ,   'No additional benefits offered']

如果还有什么可以补充的,请让我知道。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-22 10:00:41

在您的解决方案中导出一列DataFrame,需要通过选择第一列导出Series,例如按DataFrame.iloc中的位置导出

代码语言:javascript
复制
BENEFITS = pd.read_csv('../data/gri/ESG/ESG_BENEFITS.csv').iloc[:, 0].tolist()
票数 2
EN

Stack Overflow用户

发布于 2021-11-22 10:44:30

导入一个列.csv作为扁平列表。

代码语言:javascript
复制
from csv import reader
# read csv file as a list of lists
l = []
index = 0
with open('students.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Pass reader object to list() to get a list of lists
    list_of_rows = list(csv_reader)
    print(list_of_rows)
    for i in int(len(list_of_rows)):
        l.insert(index,list_of_rows[0][i])
        index+=1
print(l)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70063911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档