read csv file and return dataframe

read csv file and return dataframe

Asked on December 24, 2018 in Pandas.
Add Comment


  • 2 Answer(s)

    The csv_module can be used:

    import csv
    from pprint import pprint
    with open('foo.csv', 'rb') as f:
        reader = csv.reader(f)
        headers = reader.next()
        column = {h:[] for h in headers}
        for row in reader:
            for h, v in zip(headers, row):
                 column[h].append(v)
        pprint(column) # Pretty printer
    

    that results in:

    {'Date': ['2012-06-11',
              '2012-06-12',
              '2012-06-13',
              '2012-06-14',
              '2012-06-15',
              '2012-06-16',
              '2012-06-17'],
     'factor_1': ['1.255', '1.258', '1.249', '1.253', '1.258', '1.263', '1.264'],
     'factor_2': ['1.548', '1.554', '1.552', '1.556', '1.552', '1.558', '1.572'],
     'price': ['1600.20',
               '1610.02',
               '1618.07',
               '1624.40',
               '1626.15',
               '1626.15',
               '1626.15']}
    
    Answered on December 24, 2018.
    Add Comment

    To process with csv files the csv_module can be used:

    import csv
    with open('some.csv', 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            print row
    
    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.