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

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

How can I sort a dataframe on some column?

问题

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

英文:

I have the following code:

  1. import pandas as pd
  2. DF = pd.DataFrame({
  3. "item_id": [1, 2, 3, 4, 5, 6, 7, 8, 9],
  4. "customer": ["CG", "CGD", "CC", "CZ", "JD", "KD", "MM", "MK", "JJ"],
  5. "costs": [150, 12, 78, 56, 5, 6, 4, 68, 69],
  6. "games": [["Minecraft", "Uno"], ["WOW", "Minecraft"], ["WOW", "Minecraft"], ["WOW", "Minecraft", "The last of us"], ["Monopoly"], ["Monopoly", "Uno"], ["WOW", "Monopoly"], ["Uno", "SkipBo"], ["Schach", "Uno"]],
  7. "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"]
  8. })
  9. DF["games"] = [i[-1:] for i in DF["games"]]
  10. DF["Day"] = pd.to_datetime(DF["date"]).dt.day
  11. DF["Month"] = pd.to_datetime(DF["date"]).dt.month
  12. DF["Year"] = pd.to_datetime(DF["date"]).dt.year
  13. DF #(look at pic_1)
  14. newDF_month = DF.drop(columns=["item_id", "customer", "games", "date", "Day", "Year"])
  15. newDF_month.groupby(["Month"]).sum()

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

  1. month | costs
  2. -------------
  3. 1 | 150
  4. 2 | 78
  5. 3 | 12
  6. 9 | 69
  7. 10 | 9
  8. 11 | 62
  9. 12 | 68

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

  1. month | costs
  2. -------------
  3. 9 | 69
  4. 10 | 9
  5. 11 | 62
  6. 12 | 68
  7. 1 | 150
  8. 2 | 78
  9. 3 | 12

How can I do that?

答案1

得分: 4

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

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

输出:

  1. costs
  2. Month
  3. 9 69
  4. 10 9
  5. 11 62
  6. 12 68
  7. 1 150
  8. 2 78
  9. 3 12
英文:

You can sort_index on its shifted value modulo 12:

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

Output:

  1. costs
  2. Month
  3. 9 69
  4. 10 9
  5. 11 62
  6. 12 68
  7. 1 150
  8. 2 78
  9. 3 12

答案2

得分: 1

另一个可能的解决方案:

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

输出:

  1. 费用
  2. 9 69
  3. 10 9
  4. 11 62
  5. 12 68
  6. 1 150
  7. 2 78
  8. 3 12
英文:

Another possible solution:

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

Output:

  1. costs
  2. Month
  3. 9 69
  4. 10 9
  5. 11 62
  6. 12 68
  7. 1 150
  8. 2 78
  9. 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:

确定