O. Foss February 2016

Pandas dataframe, max of selection

I have a pandas dataframe with following columns:

I need to write a function, which takes df with data and returns string with country, which GDP is maximum among countries with area(sq km) is less than 200 OR which population is less than 1000.

How to write this code correctly?

def find_country(df):
    df.loc[((df.Area < 200).Max(df.GDP))|(df.Population < 1000)]

Answers


F. Ahrbahn February 2016

First of all you should make your first column to be your Index. This could be done using the following command:

df.set_index('Country', inlace = True)

Assuming you want to replace your dataframe with the reworked version. To find your desired country you simply look for the date which has the maximum GDP, for instance, and return its index. The subscript of the index is needed to get the actual value of the index.

def find_Country(df):
    return df[df['GDP'] == max(df['GDP'])].index[0]

I hope this will help, Fabian

Post Status

Asked in February 2016
Viewed 2,140 times
Voted 4
Answered 1 times

Search




Leave an answer