0

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

jpp
  • 159,742
  • 34
  • 281
  • 339
Aditya
  • 1
  • 1
  • @: Aditya, Welcome to SO site, However, the best practice to get answer is to show the Dataset or Dataframe you have or few line of data at least to reproduce. – Karn Kumar Jan 23 '19 at 13:33

1 Answers1

2

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]
jpp
  • 159,742
  • 34
  • 281
  • 339