Aditya
Aditya

Reputation: 1

Indexing by label with rows and columns at the same time

I want to get values of two or more different column in form of tuple

def top():
    Top15 = answer_one()
    x = Top15.loc[Top15['% Renewable'].idxmax()]
    return x.loc['% Renewable' , 'Country']

I want to get values of column named % Renewable and Country in form of a tuple

Upvotes: 0

Views: 39

Answers (1)

jpp
jpp

Reputation: 164623

pd.DataFrame.loc supports indexing by row and column labels simultaneously:

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

res = tuple(df.loc[df['A'].idxmax(), ['A', 'B']])    # (2, 4)

Or if list is sufficient:

res = df.loc[df['A'].idxmax(), ['A', 'B']].tolist()  # [2, 4]

Upvotes: 2

Related Questions