Python。查找d行的阈值

2024-04-25 15:06:36 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我是Python的新手,但我必须解决以下任务。请帮帮我。在

我有两个很长的数据列表。对于每个列表,我必须找到一个阈值,它将列表的值-1(低于阈值)和+1(高于阈值)。为了找到两组数据之间的最佳相关性,我需要将两行数据分开。一定是这样的:

List1 List2     List1 After Threshold applying  List2 After Threshold applying 
-50      -300     -1                             -1
-40      -200     -1                             -1
-30      -100     -1                             -1
-20      0        -1                             -1
-10      100       1                              1
0        200       1                              1
1        300       1                              1
2        400       1                              1

因此,在我的示例中,list1的阈值为-10(低于它的值等于-1,高于它的值等于1),而{}的阈值为100。在

非常感谢!在


Tags: 数据示例列表threshold阈值after新手list2
1条回答
网友
1楼 · 发布于 2024-04-25 15:06:36

查看python包pandas。这里有一个教程:https://pandas.pydata.org/pandas-docs/stable/tutorials.html

import pandas as pd

list1 = [-50, -40, -30, -20, -10, 0, 1, 2]
list2 = [-300, -200, -100, 0, 100, 200, 300, 400]

df = pd.DataFrame({'List 1': list1, 'List 2': list2})

newdf = df.copy()
newdf[df > df.median()] = 1
newdf[df < df.median()] = -1

newdf现在包含以下内容:

^{pr2}$

如果您希望新列表和旧列表并排,可以连接数据帧。最好先重命名列:

# rename columns:    
newdf = newdf.rename(columns=lambda x: x + ' after threshold')
# concatenate dataframes:
result = pd.concat([df, newdf], axis=1)

结果如下:

   List 1  List 2  List 1 after threshold  List 2 after threshold
0     -50    -300                      -1                      -1
1     -40    -200                      -1                      -1
2     -30    -100                      -1                      -1
3     -20       0                      -1                      -1
4     -10     100                       1                       1
5       0     200                       1                       1
6       1     300                       1                       1
7       2     400                       1                       1

相关问题 更多 >

    热门问题