I have the following dataframe:
ID Date Element Data_Value Month_Day
143873 USW00014833 2005-01-02 TMIN -0.6 01-02
74019 USW00094889 2005-01-02 TMIN -0.6 01-02
112671 USC00200032 2005-01-02 TMAX 12.2 01-02
I want to group by the "Month_Day" column and sort by the values in "Data_Value", but still keep the dataframe format and the other columns.
I have tried the following code:
df = df.groupby('Month_Day')['Data_Value'].sort_values()
However, by this method I lose the other columns and the output is a series(I believe).
Any advice? Many thanks.
IIUC you can just sort on two columns:
df = df.sort_values(['Month_Day', 'Data_Value'])
If you want to use groupby. As in the link bellow, you need to groupby using list of columns that you want to keep and then call reset_index()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.