Question:个 我正在处理一个Python项目,其中有一个包含两列的数据集:"Big List"和"Small List".我的目标是将"小列表"列中的值与"大列表"列中的值进行比较,并相应地对它们进行排序.如果在"Big List"列中找到"Small List"列中的值,我希望对"Small List"列中的相应行进行排序.此外,如果在"Big List"列中找不到"Small List"列中的值,我想将其列在一个名为"Not Match to Big List"的新列中.

"大名单"中的值不是唯一的,但"小名单"中的值是唯一的.

下面是一个例子:

Big List Small List
10 17
2 15
15 42
17 31
30 45
40 30
45
47
50

在本例中,我想将"Small List"列中的每个值与"Big List"列中的值进行比较.如果找到匹配项,我希望对"Small List"列中的相应行进行排序.如果没有找到匹配项,我想将该值列在"不匹配到大列表"列中.

预期结果:

Big List Small List Not Matched to Big List
10 31
2 42
15 15
17 17
30 30
40
45 45
47
50

当找到匹配项时如何对"Small List"列中的行进行排序,以及在没有匹配项时如何填充"Not Match to Big List"列,这让我苦苦思索.任何关于如何实现这一目标的帮助或建议都将受到高度赞赏!

以下是我到目前为止掌握的代码:

import pandas as pd

data = {'Big List': [10,2,15,17,30,40,45,47,50], 'Small List': [17,15,42,31,45,30]}
df = pd.DataFrame(data)

# Loop through rows and compare values
for index, row in df.iterrows():
    if row['Small List'] in df['Big List']:
        # Sort the row in the "Small List" column
        # Stuck here
    else:
        # Add value to "Not Matched to Big List" column
        # Stuck here

print(df)

推荐答案

现在开始(尽管我必须补充说,这个解决方案相当麻烦,因为Pandas DataFrame应该表达不同列之间的关系,而这里所做的没有任何关系):

import pandas as pd

data = {'Big List': [10,2,15,17,30,40,45,47,50], 'Small List': [17,15,42,31,45,30, None, None, None]}
df = pd.DataFrame(data)
new_df = df[['Big List']].merge(df[['Small List']], left_on='Big List', right_on='Small List', how='left')
new_df.sort_values(by='Big List', inplace=True)
notfound_df = df[['Big List']].merge(df[['Small List']], left_on='Big List', right_on='Small List', how='right')
notfound_df = notfound_df[notfound_df['Big List'].isna()].dropna(how='all')
notfound_list = notfound_df['Small List'].to_list()
for i in range(new_df.shape[0] - len(notfound_list)):
    notfound_list.append(None)
new_df = pd.concat([new_df, pd.DataFrame(notfound_list, columns=['Not Matched to Big List'])], axis=1)
new_df

   Big List  Small List  Not Matched to Big List
1         2         NaN                     31.0
0        10         NaN                     42.0
2        15        15.0                      NaN
3        17        17.0                      NaN
4        30        30.0                      NaN
5        40         NaN                      NaN
6        45        45.0                      NaN
7        47         NaN                      NaN
8        50         NaN                      NaN

Python-3.x相关问答推荐

正确的本地react 方式-Django身份验证

使用 iloc 或 loc 对多列进行过滤

嵌套协议的使用(协议成员也是协议)

numpy是如何添加@运算符的?

Snakemake 'run' 指令不产生错误信息

合并问卷中多列中的稀疏问题 - Pandas

在判断列表变量时如何判断特定列的值并分配加权整数值

过滤查询集和Q运算符的不同值

如何确定一个类的元类?

全局捕获快速 api 中的异常

如何在 Python 中计算两个包含字符串的列表的 Jaccard 相似度?

如何在不使用 @hydra.main() 的情况下获取 Hydra 配置

tkinter TclError:错误的文件类型使用 askopenfilename

根据条件过滤元组列表

如何将二进制(字符串)转换为浮点值?

Python 3 - Zip 是 pandas 数据框中的迭代器

警告:请使用 tensorflow/models 中的官方/mnist/dataset.py 等替代方案

Python 3 中的连接列表

在 linux mint 上安装 python3-venv 模块

Django 教程 unicode 不起作用