I made nested list
information = [['name', 'age', 'sex', 'height', 'weight'], ['sam', '17', 'm', 155, 55], [...]] to make table.
and I want to sort data according to height. but when I use .sort()
method, because of information[0]
, this error message comes out
"TypeError: '<' not supported between instances of 'float' and 'str'"
how can I sort data except first element?
As you can see first picture, i made table to see in excel file. ans I want to sort elements according to G column. (this is the code of movie info) but when I tried sorting using sort(), because the top element is "러닝타임 평점"(str type), sorting is not able.
CodePudding user response:
You can use sorted
with the 4th element as key on a slice of information
that doesn't hold the headers
information[1:] = sorted(information[1:], key=lambda x: x[3])
CodePudding user response:
if using of pandas library is acceptable then you can do this create as dataframe.
import pandas as pd
df = pd.DataFrame(information)
# make the first row as column names
headers = df.iloc[0]
# create a updated dataframe with those new column names
updated_df = pd.DataFrame(df.values[1:], columns=headers)
# sort the values based on height
updated_df.sort_values('height', inplace=True)