Question:个 我正在处理一个Python项目,其中有一个包含两列的数据集:"Big List"和"Small List".我需要将"小列表"列中的值与"大列表"列中的值进行比较,并相应地对它们进行排序.如果在"Big List"列中找到"Small List"列中的值,我希望对"Small List"列中的相应行进行排序.
例如,假设我有以下数据:
Big List | Small List |
---|---|
10 | 20 |
20 | 30 |
30 | 60 |
40 | 0 |
50 | 0 |
60 | 0 |
20 | 0 |
在这个场景中,我打算将"小列表"列中的每个值与"大列表"列中的值进行比较.然后,我将在"大列表"列中的相应值匹配的同一行的"小列表"中对这些值进行排序.
What I want:个
Big List | Small List |
---|---|
10 | 0 |
20 | 20 |
30 | 30 |
40 | 0 |
50 | 0 |
60 | 60 |
20 | 20 |
What I've Tried:个 我已经研究过使用循环和条件语句,但我在弄清楚如何有效地比较和排序这两列之间的值时遇到了麻烦.
import pandas as pd
data = {'Big List': [10, 20, 30, 40,50,60,20], 'Small List': [20,30,60]}
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
df