You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何筛选第二个DataFrame中小于第一个对应日期的数值?

Solution: Retrieve df2 Records Where Windspeed is Lower Than df1's Corresponding Value

First, let's fix a critical issue: your windspeed columns are stored as strings, not numeric values. Comparing strings directly will give incorrect results (e.g., '3' > '29' lexicographically), so we need to convert them to integers first.

Here's the step-by-step approach:

Step 1: Convert Windspeed Columns to Numeric Types

import pandas as pd
import numpy as np

# Your original data
data1 = {'Date': {1: '1/1/2008', 2: '1/2/2008', 3: '1/3/2008', 4: '1/4/2008', 5: '1/5/2008', 6: '1/6/2008', 7: '1/7/2008', 8: '1/8/2008', 9: '1/9/2008', 10: '1/10/2008' }, 'windspeed': {1: '36', 2: '38', 3: '40', 4: '39', 5: '45', 6: '33', 7: '31', 8: '39', 9: '41', 10: '37'}}
df1 = pd.DataFrame(data1)
data2 = {'Date': {1: '1/1/2008', 2: '1/2/2008', 3: '1/3/2008', 4: '1/4/2008', 5: '1/5/2008', 6: '1/6/2008', 7: '1/7/2008', 8: '1/8/2008', 9: '1/9/2008', 10: '1/10/2008' }, 'windspeed': {1: '33', 2: '39', 3: '42', 4: '35', 5: '43', 6: '40', 7: '39', 8: '37', 9: '44', 10: '35'}}
df2 = pd.DataFrame(data2)

# Convert windspeed from string to integer
df1['windspeed'] = df1['windspeed'].astype(int)
df2['windspeed'] = df2['windspeed'].astype(int)

Step 2: Create a Boolean Mask and Filter df2

Now we can compare the numeric columns directly. We'll create a mask where df2['windspeed'] is less than df1['windspeed'], then use that mask to extract the relevant rows from df2:

# Create boolean mask
mask = df2['windspeed'] < df1['windspeed']

# Get the full records from df2 where the condition is true
result_records = df2[mask]

# If you only want the windspeed values (as a Series)
result_windspeed = df2.loc[mask, 'windspeed']

Output

Running print(result_records) will give you:

Date  windspeed
1   1/1/2008         33
4   1/4/2008         35
5   1/5/2008         43
8   1/8/2008         37
10 1/10/2008         35

And print(result_windspeed) gives:

1     33
4     35
5     43
8     37
10    35
Name: windspeed, dtype: int64

Why This Works

  • Converting to numeric types ensures we're doing actual numerical comparison instead of string lexicographical comparison.
  • The boolean mask acts as a filter: only rows where the mask is True are kept in the result.
  • Using df2[mask] returns the full rows from df2 that meet your condition, not just the boolean values.

内容的提问来源于stack exchange,提问作者rajeev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:38:40