如何在Pandas中根据另一个框架中的列对框架进行排序?

9avjhtql  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(82)

假设我有两个dataframe,df1和df2在上面的图片。我想根据df2的Col2对df1进行排序。
因此,df1的最终结果应该类似于图中的第三个点阵,其中Col2的值相同,在df2中排序。

fnx2tebb

fnx2tebb1#

您可以使用set_indexreindex的组合来执行此操作。
试试这个代码:

df1 = pd.DataFrame({'Col1': ['a','b','c','d','e'], 'Col2': 
['chocolate','chicken','pizza','icecream','cake'] })
Out :
  Col1       Col2
0    a  chocolate
1    b    chicken
2    c      pizza
3    d   icecream
4    e       cake
df2 = pd.DataFrame({'Col1': ['f','g','h','i','j'], 'Col2': ['chicken','cake','icecream','chocolate','pizza'] })
Out :
  Col1       Col2
0    f    chicken
1    g       cake
2    h   icecream
3    i  chocolate
4    j      pizza
df1 = df1.set_index('Col2')
df1 = df1.reindex(index=df2['Col2'])
df1 = df1.reset_index()
Out :
        Col2 Col1
0    chicken    b
1       cake    e
2   icecream    d
3  chocolate    a
4      pizza    c
wkftcu5l

wkftcu5l2#

请试试这个:

import pandas as pd

# Assume we have the following two dataframes:
df1 = pd.DataFrame({'Col1': ['a','b','c','d','e', 'f'], 
                    'Col2': ['chocolate','chicken','pizza','icecream','cake', 'pizza'],
                    'Col3': [1, 2, 3, 4, 5, 6],
                    'Col4': ['x', 'y', 'z', 'xx', 'yy', 'zz']})
df2 = pd.DataFrame({'Col1': ['f','g','h','i','j'], 
                    'Col2': ['chicken','cake','icecream','chocolate','pizza']})

# Create a dictionary from df2 with 'Col2' as keys and 'Col1' as values
order_dict = df2.set_index('Col2')['Col1'].to_dict()

# Create a new column in df1 that maps the values in 'Col2' to the values in the dictionary
df1['sort_column'] = df1['Col2'].map(order_dict)

# Sort df1 based on the new column, then drop the new column
df1 = df1.sort_values('sort_column').drop(columns='sort_column')

print(df1)

输出:

Col1       Col2  Col3 Col4
1    b    chicken     2    y
4    e       cake     5   yy
3    d   icecream     4   xx
0    a  chocolate     1    x
2    c      pizza     3    z
5    f      pizza     6   zz

相关问题