Обычно я знаю, как сортировать по определенному столбцу:
Код: Выделить всё
df.sort_values(by='TOEFL Score', ascending=False)
Попытка моего кода:
Код: Выделить всё
import pandas as pd
# Sample data setup
data = {
'Serial No.': [1, 2, 3],
'GRE Score': [337, 324, 316],
'TOEFL Score': [118, 107, 104]
}
df = pd.DataFrame(data)
# Reading the column name from user
col_name = input()
# I am trying to sort by 'col_name' and get the top 5 rows
# This is where I am stuck.
# Does sort_values accept the string variable directly?
sorted_df = df.sort_values(by=col_name, ascending=False)
print(sorted_df.head())
Подробнее здесь: https://stackoverflow.com/questions/798 ... a-variable
Мобильная версия