如何按某列对数据框进行排序?

huangapple go评论96阅读模式
英文:

How can I sort a dataframe on some column?

问题

我想按照以下顺序对数据进行排序:(9, 10, 11, 12, 1, 2, 3),你能帮我做到吗?

英文:

I have the following code:

import pandas as pd
DF = pd.DataFrame({
    "item_id": [1, 2, 3, 4, 5, 6, 7, 8, 9],
    "customer": ["CG", "CGD", "CC", "CZ", "JD", "KD", "MM", "MK", "JJ"],
    "costs": [150, 12, 78, 56, 5, 6, 4, 68, 69],
    "games": [["Minecraft", "Uno"], ["WOW", "Minecraft"], ["WOW", "Minecraft"], ["WOW", "Minecraft", "The last of us"], ["Monopoly"], ["Monopoly", "Uno"], ["WOW", "Monopoly"], ["Uno", "SkipBo"], ["Schach", "Uno"]], 
    "date": ["2022-01-18", "2022-03-12", "2022-02-26", "2021-11-12", "2021-10-09", "2021-11-26", "2021-10-18", "2021-12-22", "2021-09-06"]
})

DF["games"] = [i[-1:] for i in DF["games"]]
DF["Day"] = pd.to_datetime(DF["date"]).dt.day
DF["Month"] = pd.to_datetime(DF["date"]).dt.month
DF["Year"] = pd.to_datetime(DF["date"]).dt.year
DF #(look at pic_1)

newDF_month = DF.drop(columns=["item_id", "customer", "games", "date", "Day", "Year"])
newDF_month.groupby(["Month"]).sum()

But this presents the data ordered by month using the ordering (1,2,3,9,10,11,12):

month | costs
-------------
1  | 150
2  |  78
3  |  12
9  |  69
10  |   9
11  |  62
12  |  68

but I would like to have the ordering be (9, 10, 11, 12, 1, 2, 3) instead:

month | costs
-------------
9  |  69
10  |   9
11  |  62
12  |  68
1  | 150
2  |  78
3  |  12

How can I do that?

答案1

得分: 4

你可以对其移位后取模12来使用 sort_index

(newDF_month
 .groupby("Month").sum()
 .sort_index(key=lambda i: (i-9)%12)
)

输出:

       costs
Month       
9         69
10         9
11        62
12        68
1        150
2         78
3         12
英文:

You can sort_index on its shifted value modulo 12:

(newDF_month
.groupby(["Month"]).sum()
.sort_index(key=lambda i: (i-9)%12)
)

Output:

       costs
Month       
9         69
10         9
11        62
12        68
1        150
2         78
3         12

答案2

得分: 1

另一个可能的解决方案:

= 新DF_月.groupby(["月"]).sum()
m =.index >= 9
pd.concat([新.loc[m, :], 新.loc[~m, :]])

输出:

       费用
9       69
10       9
11      62
12      68
1      150
2       78
3       12
英文:

Another possible solution:

new = newDF_month.groupby(["Month"]).sum()
m = new.index >= 9
pd.concat([new.loc[m, :], new.loc[~m, :]])

Output:

       costs
Month       
9         69
10         9
11        62
12        68
1        150
2         78
3         12

huangapple
  • 本文由 发表于 2023年4月16日 23:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定