python - 按特定顺序重新组织数据框

标签 python python-3.x pandas dataframe

大家好,我有 2 个数据框,我正在尝试按特定顺序合并和分组:

df1   
   LC_REF     Category      PRDGRP
0  17 1C      H         Ferrari,Lambo,Merc
1  17 1C      M         Doritos,Lays,Funyun
2  17 1C      P         Cats,Dogs,Rabbits
3  16 2C      H         Aston,Hyundai,Honda
4  16 2C      M         Cheeto, Vicks
5  16 2C      P         Rat,Pig,Flamingo
6  17 2C      M         See,Sea,Far


df2   
   LC_REF     Category      PRDGRP
0  17 1C         H         foo,bar
1  17 1C         M         foo,bar1
2  16 2C         H         foo,bar2
3  16 2C         M         foo,bar3
4  17 2C         H         foo,bar4
5  17 2C         M         foo,bar5
6  17 2C         P         foo,bar6

我正在寻找合并它们,以便一个 LC_REF 的所有 M 都堆叠起来,然后是所有 H,然后是所有 P,然后移动到第二个 LC_REF。顺序并不重要,但应该保持一致。希望这是有道理的:

df3   
   LC_REF     Category      PRDGRP
0  17 1C       M         Doritos,Lays,Funyun
1  17 1C       M         foo,bar1
2  17 1C       H         Ferrari,Lambo,Merc
3  17 1C       H         foo,bar
4  17 1C       P         Cats,Dogs,Rabbits
5  16 2C       M         Cheeto, Vicks
6  16 2C       M         foo,bar3
7  16 2C       H         Aston,Hyundai,Honda
8  16 2C       H         foo,bar4
9  17 2C       M         See,Sea,Far
10  17 2C      M         foo,bar5
11  17 2C      P         foo,bar6

我尝试了 concat 和append 的变体,但没有成功:

pd.concat([df1,df2]).sort_index().reset_index(drop=True)

几乎接近,但 LC_REF 出现故障

最佳答案

让我们使用pd.concatsort_values:

df_out = pd.concat([df1,df2])
df_out['Category'] = df_out.Category.astype('category', categories=['M','H','P'], ordered=True)
df_out.sort_values(by=['LC_REF','Category'])

输出:

  LC_REF Category               PRDGRP
4  16 2C        M        Cheeto, Vicks
3  16 2C        M             foo,bar3
3  16 2C        H  Aston,Hyundai,Honda
2  16 2C        H             foo,bar2
5  16 2C        P     Rat,Pig,Flamingo
1  17 1C        M  Doritos,Lays,Funyun
1  17 1C        M             foo,bar1
0  17 1C        H   Ferrari,Lambo,Merc
0  17 1C        H              foo,bar
2  17 1C        P    Cats,Dogs,Rabbits
6  17 2C        M          See,Sea,Far
5  17 2C        M             foo,bar5
4  17 2C        H             foo,bar4
6  17 2C        P             foo,bar6

关于python - 按特定顺序重新组织数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45493279/

相关文章:

python - 将类别列表打印为一列

python - 如何注释堆积图或面积图

python - 如何将 blob 文件转换为特定格式?

python - Django : custom management command not registered using app config

python - 尝试使用正则表达式提取字符串 - Python

regex - 关于使用正则表达式的说明

python - 如何使用Python3检查rpm GPG签名?

Python:如何将不同数据帧中具有相同名称的列彼此相邻放置?

python - 循环中的 QtCore.QObject.connect 仅影响最后一个实例

python - 如何在 Python 函数中返回左值?