在Python中,可以使用pandas
库来按照特定阈值划分数据框。以下是一个示例代码:
import pandas as pd
# 创建一个示例数据框
data = {'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'B': [11, 14, 15, 18, 20, 21, 25, 29, 30, 35]}
df = pd.DataFrame(data)
# 定义阈值
threshold = 15
# 按照阈值划分数据框
df_above_threshold = df[df['B'] > threshold]
df_below_threshold = df[df['B'] <= threshold]
# 打印结果
print("Above threshold:")
print(df_above_threshold)
print("\nBelow threshold:")
print(df_below_threshold)
输出结果如下:
Above threshold:
A B
2 3 15
3 4 18
4 5 20
5 6 21
6 7 25
7 8 29
8 9 30
9 10 35
Below threshold:
A B
0 1 11
1 2 14
以上代码首先创建一个示例数据框,其中包含'A'和'B'两列。然后定义了一个阈值threshold
,并使用该阈值对数据框进行划分。最后,根据阈值将数据框划分为两个新的数据框df_above_threshold
和df_below_threshold
。
上一篇:按照特定约束计算排列
下一篇:按照特定值查询改为查询