在 pandas 中,如何按照自定义规则对列按值进行分组排序。

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

how in pandas groupby custom sort columns by value

问题

I have a dataframe and I try the results from Groupby export to excel by custom sort values one, two or more columns:

  • id sorted by ['bv', 'cl', 'an']
  • color sorted by ['blue', 'red', 'orange', 'green']
import pandas as pd
data = {'color': ['green', 'red', ' orange', 'red','blue',' orange', 'red','blue', 'red', ' orange', 'red','red','green'],
        'Name': ['Tom', 'nick', 'krish', 'jack','bob','Tom', 'nick', 'krish', 'nick', 'krish', 'jack','nick','Tom'],
        'form': [ 'a', 'b', 'c', 'd','e','b', 'c', 'd','d','e','b', 'c', 'b'],
        'id': [ 'an', 'bv', 'cl', 'cl','an','bv', 'an', 'cl','cl','an','bv','bv','an'],
        }

df = pd.DataFrame(data)

print(df)

df_1 = df.groupby(['color','Name','id'])['form'].agg('count')
print(df_1)

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')    
df_1.to_excel(writer, sheet_name='Sheet1')
writer.save()

print('I try the df to excel have custom sort values in columns')
print('id sort by [bv,cl,an]')
print('color sort by [blue,red,orange,green]')
print('how can make it?')

(Note: I have provided the code without translation as requested.)

英文:

I have a dataframe and i try the results from Groupby export to excel by custom sort values one , two or more columns :

  • id sorted by ['bv','cl','an']
  • color sorted by ['blue','red','orange','green']
import pandas as pd
data = {'color': ['green', 'red', ' orange', 'red','blue',' orange', 'red','blue', 'red', ' orange', 'red','red','green'],
        'Name': ['Tom', 'nick', 'krish', 'jack','bob','Tom', 'nick', 'krish', 'nick', 'krish', 'jack','nick','Tom'],
        'form': [ 'a', 'b', 'c', 'd','e','b', 'c', 'd','d','e','b', 'c', 'b'],
        'id': [ 'an', 'bv', 'cl', 'cl','an','bv', 'an', 'cl','cl', 'an','bv','bv','an'],
        }

df = pd.DataFrame(data)

print(df)

df_1 = df.groupby(['color','Name','id'])['form'].agg('count')
print(df_1)

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')    
df_1.to_excel(writer, sheet_name='Sheet1')
writer.save()

print(' I try the df to excel have custom sort values in columns')
print('id sort by [bv,cl,an]')
print('color sort by [blue,red,orange,green]')
print('how can make it?')

答案1

得分: 1

You can use 分类数据 然后 sort_index

ids = pd.CategoricalDtype(["bv", "cl", "an"], ordered=True)
colors = pd.CategoricalDtype(["blue", "red", "orange", "green"], ordered=True)
# 如有需要,添加更多类别

out = (
    df.astype({"id": ids, "color": colors})
        .groupby(["color", "Name", "id"], observed=True)["form"].agg("count")
        .sort_index(level=[0, 2]) # 如有需要,添加更多级别
)

with pd.ExcelWriter("test.xlsx", engine="xlsxwriter") as writer:
    out.to_excel(writer, sheet_name="Sheet1")
    # 如有需要,添加自定义样式/格式化

输出:

print(out)

color   Name   id
blue    krish  cl    1
        bob    an    1
red     jack   bv    1
        nick   bv    2
        jack   cl    1
        nick   cl    1
               an    1
orange  Tom    bv    1
        krish  cl    1
               an    1
green   Tom    an    2
Name: form, dtype: int64
英文:

You can use Categorical Data then sort_index :

ids = pd.CategoricalDtype(["bv", "cl", "an"], ordered=True)
colors = pd.CategoricalDtype(["blue", "red", "orange", "green"], ordered=True)
#add here more categories if needed

out = (
    df.astype({"id": ids, "color": colors})
        .groupby(["color", "Name", "id"], observed=True)["form"].agg("count")
        .sort_index(level=[0, 2]) #in case, add here more levels
)

with pd.ExcelWriter("test.xlsx", engine="xlsxwriter") as writer:
    out.to_excel(writer, sheet_name="Sheet1")
    # add here a custom styling/formatting if needed

Output :

print(out)

color   Name   id
blue    krish  cl    1
        bob    an    1
red     jack   bv    1
        nick   bv    2
        jack   cl    1
        nick   cl    1
               an    1
orange  Tom    bv    1
        krish  cl    1
               an    1
green   Tom    an    2
Name: form, dtype: int64

huangapple
  • 本文由 发表于 2023年6月18日 19:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500410.html
匿名

发表评论

匿名网友

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

确定