James Graves
James Graves

Reputation: 57

Create a list within list for specific changes in index

What I have is a list (large one) from an imported CSV file that had a bunch of data. It is weather data spanning over an entire year. So 12 months of data. the months are represented as 1 - 12: please look at the image:

enter image description here

Please note I CAN NOT import any modules!

As you can see, the number after the year in each index is the month (eg. 01, 02, 03 etc) What I want to do is create a list for each month within this list e.g. [['2011,01,...,...],[2011,02,...,...]].

How do I go about doing that?

My current code for opening and adding the CSV data to a list:

try:
    fp = open(filename, "r")
    data = []
    for line in fp:
        line = line.strip()
        cols = line.split(',')
        cols = cols[1:]
        data.append(','.join(cols))



    fp.close()

    print(data[1:])


except IOError:
     print("Error!!! Opening file...")

Upvotes: 0

Views: 43

Answers (3)

Jurgen Strydom
Jurgen Strydom

Reputation: 3930

Since you are not allowed to import, create a dict for each month that has an empty array as a value in that dict. Then load the data into the dict, appending the data to the array for that month.

month_dict = {}
for k in range(12):
    month_dict[k+1] = []

try:
    fp = open('example.csv', "r")
    data = []
    for line in fp:
        print(line)
        line = line.strip()
        cols = line.split(',')
        cols = cols[1:]
        month_dict[int(cols[1])].append(cols)
    fp.close()
    print(month_dict)
except IOError:
     print("Error!!! Opening file...")

To get the data list for that month read the dict[month]. Remember to convert to ints.

Upvotes: 0

Jurgen Strydom
Jurgen Strydom

Reputation: 3930

This looks like a job for the pandas package pip3 install pandas.

With pandas you can read csv files with the read_csv function. Once you have that dataframe you can select all months.

Here is some example code:

import pandas as pd

df = pd.DataFrame([['2012', '01', '29'],
                   ['2012', '01', '30'],
                   ['2012', '01', '31'],
                   ['2012', '02', '01'],
                   ['2012', '02', '02'],
                   ['2012', '02', '03'],],
                 columns=['year', 'month', 'day'])
df[df['month']=='01']

Which outputs:

    year    month   day
0   2012    01      29
1   2012    01      30
2   2012    01      31

To read the csv you can use this code in the place of my example dataframe:

df = pd.read_csv('example.csv')

I would recommend you use pandas for what you want to do from there on. But if you absolutely need your data as a list of lists you can do:

list(df[df['month']=='01'].apply(lambda x: x.tolist(), axis=1))

Which outputs:

[['2012', '01', '29'], ['2012', '01', '30'], ['2012', '01', '31']]

Upvotes: 1

ErikXIII
ErikXIII

Reputation: 557

Try to use the csv module in python and read it as a dictionary like this

import csv
def read_csv_to_dict(file_path):
    with open(file_path) as f:
        a = [{k: v for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True, delimiter=',')]
    return a

data = read_csv_to_dict(filename)

Then data is a list of dictionaries with one line per row.

I think that is a good start, paste the file if you need more help.

Upvotes: 0

Related Questions