New column add question - help please

INITIAL CODE

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

for app in apps_data[1:]:
price = float(app[4])
if price == 0.0:
app.append(‘free’)
else:
app.append(‘non-free’)

apps_data[0].append(‘free_or_not’)
print(apps_data[:6])

Does it mean that new var containing values ‘free’ and ‘non-free’ will be placed as a first vaiable in this dataset ? and here: apps_data[0].append(‘free_or_not’) it is renamed ?

Regards,
Andy

1 Like

Hi Andy,

Welcome to the Community!

Does it mean that new var containing values ‘free’ and ‘non-free’ will be placed as a first vaiable in this dataset?

In this for-loop we iterate through the list of lists apps_data starting from its second list (hence [1:]) and to each list (app), we append at the end a string “free” or “non-free” according to the condition.

and here: apps_data[0].append(‘free_or_not’) it is renamed

No, it’s not renamed. In the for-loop, we were iterating through the list of lists starting from the second list. Hence, till now, we never modified anything in the first list (the one with the index 0). Practically, this zero-indexed list is a list of column names for all the subsequent lists. Running this code:

apps_data[0].append('free_or_not') 

we append to this list the string “free_or_not”, which can be considered as a name of the new column that we created earlier inside the for-loop.

For your future questions, please consider adding the corresponding mission link and enclosing your code into backticks. You can find useful this guide. Thank you!

HI Elena

Thanks for this. So I understand that when variable is created of the back of conditional statement, it will put placed at the end of file (as a last column) - and it will not have header name.

Now - what I do not understand is and here: apps_data**[0]**.append(‘free_or_not’) why we append name for this column at the beggining of our list ? and not at the end

Regards,
Andy

1 Like

Hi Andy,

This apps_data is practically a giant list of lists. For convenience, let’s call those “small lists” of which apps_data consists, sub-lists. The very first sub-list (the one with the index 0, i.e. apps_data[0]) is a bit specific and not like all the others: it represents, let’s say, a list of column names, while all the other sub-lists represent rows of a table. These terms (columns, rows, column names, table, etc) will make more sense for you in one of the next courses, on pandas library.

In the for-loop, we iterate through the sub-lists starting from the second one (hence [1:]) , i.e. for now we ignore that “strange” first sub-list (the list of column names). To each sub-list, we append a string “free” or “non-free” at the end, depending on the condition. At the end of the for-loop, we will have all the sub-lists modified (+1 item to the end of each one), apart from the very first sub-list that we didn’t tough until now. Now, we take this sub-list separately apps_data[0] (0 - because it has a 0-index in the indexing system of the giant list of lists, and not because we want to add something at its beginning) and add to its end (since the method append() always adds an item at the end of a list) the string 'free_or_not', already without checking any conditions. This string can be considered as a name of the newly created column. All the sub-lists of our giant list of lists have the same number of items now, including the first sub-list with the column names.

Perfect - I do understand it now - thank you very much Elena.

1 Like