Home > OS >  pd.read_csv() imports list of lists
pd.read_csv() imports list of lists

Time:11-22

Goal: import one column .csv as a flattened list.

ESG_BENEFITS.csv:

import pandas pd

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

print(BENEFITS)

Output:

[['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']]

Desired Output: Flattened list, ignore extra spacing.

['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']

Please let me know if there is anything else I can add to post.

CodePudding user response:

In your solution is exported one column DataFrame, need export Series by select first column, e.g. by position with DataFrame.iloc:

BENEFITS = pd.read_csv('../data/gri/ESG/ESG_BENEFITS.csv').iloc[:, 0].tolist()

CodePudding user response:

Importing one column .csv as a flattened list.

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)
  • Related