Luis Valencia
Luis Valencia

Reputation: 33998

How to get the percentage of one value across a column in pandas

I have a dataframe with something simple:

Country % Renewable
USA 100
Belgium 90
UK 103

I need to get two things:

  1. Which country has the biggest Energy; I did this:
    df = df.sort_values(by=['% Renewable'],ascending=False).head(1)
  1. the percentage of that value across all countries and return it in a tuple with (Country Name, %)

How can I achieve this?

Update 1: I did this:

def answer_six():
    df = answer_one()
    #print(df)
    suma = df['% Renewable'].sum()
    top3 = df['% Renewable'].nlargest(1)
    tups = list(zip(top3.index, top3.div(suma)*100))
    return tups

However the autograder says I need to return a tuple, isnt it already?

Upvotes: 0

Views: 94

Answers (2)

Vaibhav Krishna
Vaibhav Krishna

Reputation: 11

Assuming your df contains one column of 'country' and another column of '% Renewable'. After you have done sorting (without using head(1)):

df = df.sort_values(by=['% Renewable'],ascending=False)

simply use a for loop:

for i in range(len(df)):
    c = (df.loc[i,'country'],df.loc[i,'% Renewable'])
    print(c)

If you want to save as a tuple:

t = list(zip(df['country'],df['% Renewable']))
print(t)

If you are only interested in printing as in (country,% Renewable), then simply use list comprehension:

[print(df.loc[i,'country'],df.loc[i,'% Renewable']) for i in range(len(df))]

Since you have already sorted the df, it will print in descending order.

Upvotes: 1

jezrael
jezrael

Reputation: 862661

Use max and then for tuples zip divided column and convert to list:

top = df['% Renewable'].max()
print (top)
103

If Country is index and need top3 countries use:

top3 = df['% Renewable'].nlargest(3)
tups = list(zip(top3.index, top3))
print (tups)

Upvotes: 1

Related Questions