import pandas as pd import numpy as np data1 = [1, 3, 2, 4] data2 = [1015, 1014, 1014, 1019] data3 = [33, 39, 91, 14] data = [data1, data2, data3] print('data: ', data, "\n") data_transposed = np.transpose(data) print('transposed data: \n', data_transposed, "\n") df = pd.DataFrame(data_transposed, columns=['quantity', 'date', 'price']) print('DataFrame: \n', df, "\n") size = len(df['date']) evaluation = [df['quantity']*df['price']] temp = np.array(evaluation).reshape(size,1) df = np.hstack([df, temp]) df = pd.DataFrame(df, columns=['quantity', 'date', 'price', 'evaluation']) print("DataFrame with added column: \n", df, "\n") df = df.sort_values(['date', 'evaluation'], ascending=[True, True]) df = df[["date", "evaluation", "price", "quantity"]] print('sorted DataFrame: \n', df, "\n") df = df.reset_index(drop=True) print('re-indexed data: \n', df)
Aug 9, 2021
[Python] to reset index for a sorted list
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment