SEN1221TUD / Q2_2023

3 stars 4 forks source link

Exercise 1 Workshop 1 #7

Open allis103 opened 7 months ago

allis103 commented 7 months ago

Hi,

I was trying to spice up the basic code for exercise 1 by printing only the columns which have a value different from 1 then the column with the highest value. However I am incurring in some problems. This is my code :

print('Columns with missing values sorted in descending order:\n') 
i = df.apply(lambda x: sum(x.eq(99999)), axis=0).sort_values(ascending=False) #counts how many times each row has a 99999 in it
#this part aims to print only the column names of columns which have an i different from 0<br>
for element in i:
    if element !=0:
        print (element, i.iloc[0])

Do any of you know why i is a list and not a matrix (it has a single column) and so how it can be linked to the attribute names in order to only print those ?

Thanks !

gnova3 commented 7 months ago

I think you can try these two alternatives:

for element, valor in i.items(): if valor !=0: print(f"{element}, {valor}") or:

print('Columns with missing values sorted in descending order:\n') i = df.apply(lambda x: sum(x.eq(99999)), axis=0).sort_values(ascending=False) i[i>0]