We have answer of your question!

100% solved queries, no empty question

Question: Modifying column of 2d list while iterating over it in python


0

I am trying to write a function that turns all the non-numerical columns in a data set to numerical form.

The data set is a list of lists.

Here is my code:

def handle_non_numerical_data(data):
    def convert_to_numbers(data, index):
        items = []
        column = [line[0] for line in data]
        for item in column:
            if item not in items:
                items.append(item)
        [line[0] = items.index(line[0]) for line in data]
        return new_data

    for value in data[0]:
        if isinstance(value, str):
            convert_to_numbers(data, data[0].index(value))

Apparently [line[0] = items.index(line[0]) for line in data] is not valid syntax and I cant figure out how to modify the first column of data while iterating over it.

I can't use numpy because the data will not be in numerical form until after this function is run.

How do I do this and why is it so complicated? I feel like this should be way simpler than it is...

In other words, I want to turn this:

[[M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15],
[M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7],
[F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9]]

into this:

[[0,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15],
[0,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7],
[1,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9]]

Note that the first column was changed from strings to numbers.

Question author Matt-d | Source

Answer


1


Solution

data = [['M',0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15],
        ['M',0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7],
        ['F',0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9]]

values = {'M': 0, 'F': 1}

new_data = [[values.get(val, val) for val in line] for line in data]
new_data

Output:

[[0, 0.455, 0.365, 0.095, 0.514, 0.2245, 0.101, 0.15, 15],
 [0, 0.35, 0.265, 0.09, 0.2255, 0.0995, 0.0485, 0.07, 7],
 [1, 0.53, 0.42, 0.135, 0.677, 0.2565, 0.1415, 0.21, 9]]

Explanation

You can take advantage of Python dictionaries and their get method.

These are values for the strings:

values = {'M': 0, 'F': 1}

You can also add more strings like I with a corresponding value.

If the string is values, you will get the value from the dict:

>>> values.get('M', 'M')
0 

Otherwise, you will get the original value:

>>> values.get(10, 10)
10
Answer author Mike-m%c3%bcller

Tickanswer.com is providing the only single recommended solution of the question Modifying column of 2d list while iterating over it in python under the categories i.e python , list , data-type-conversion , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!