根据阈值更改双索引数据框中的值

因此,我正在使用一个非常大的数据集,但我正在使用一个小得多的数据集作为我需要做的模板。假设我有以下 2 个数据框:


import numpy as np

import pandas as pd

df = pd.DataFrame({

   'cond': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B','B', 'B', 'B', 'B', 'B','B','B'],

   'Array':  ['S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','S', 'S', 'TT', 'TT','SS','TT'],

   'X':  [1, 2, 3, 1, 2 , 3, 4, 7.3, 5.1, 3.2, 1.4, 5.5, 9.9, 3.2, 1.1, 3.3, 1.2, 5.4],

   'Y':  [3.1, 2.2, 2.1, 1.2,  2.4, 1.2, 1.5, 1.33, 1.5, 1.6, 1.4, 1.3, 0.9, 0.78, 1.2, 4.0, 5.0, 6.0],

   'Marker':  [2.0, 1.2, 1.2, 2.01, 2.55, 2.05, 1.66, 3.2, 3.21, 3.04, 8.01, 9.1, 7.06, 8.1, 7.9, 5.12, 5.23, 5.15],

   'Area': [3.0, 2.0, 2.88, 1.33,  2.44, 1.25, 1.53, 1.0, 0.156, 2.0, 2.4, 6.3, 6.9, 9.78, 10.2, 15.0, 16.0, 19.0]

})

print(df)


df2 = pd.DataFrame({

   'cond': ['A', 'A', 'B', 'B', 'B'],

   'Array':  ['S', 'TT', 'S', 'SS','TT'],

   'cutoff1':  [2.55, 2.01, 7.06, 1, 8.01],

   'cutoff2':  [1.60, 2.2, 2.1, 1.2,  2.4]

})

print(df2)

这会产生以下两组:


   cond Array    X     Y  Marker    Area

0     A     S  1.0  3.10    2.00   3.000

1     A     S  2.0  2.20    1.20   2.000

2     A    TT  3.0  2.10    1.20   2.880

3     A    TT  1.0  1.20    2.01   1.330

4     A     S  2.0  2.40    2.55   2.440

5     A     S  3.0  1.20    2.05   1.250

6     A    TT  4.0  1.50    1.66   1.530


我想做的是使用 df2 中的截止值来修改我的原始数据集(df)。我要做的是将df中的所有“标记”值转换为0或1。目标是创建另外两个数据帧,一个使用cutoff1作为阈值,一个使用cutoff2作为阈值。例如,对于 cutoff1,由于 AS 配对的截止值为 2.55,我想创建一个新的数据帧,其中标记值 <=2.55 的所有 AS 配对设置为 0,BS 配对的值<=7.06 设置为 0 等,df 中的其他所有内容保持不变。同样,我想制作第二个数据框,其中完成了相同的操作,但用于 cutoff2 值。


我试图在堆栈溢出中搜索已经完成的模型,我可以适应我的模型,但我似乎只找到了在一个阈值上更改单个列中的所有值的模型(例如这里:最有效的转换方式Pandas DataFrame 中列的值),而这里有一列有多个截止值,这是基于另外两列的索引。


心有法竹
浏览 129回答 1
1回答

肥皂起泡泡

您可以分别为每个截止点执行以下操作:df = df.set_index(['cond', 'Array'])result = df.merge(df2, on=['cond', 'Array'])result.loc[result.Marker < result.cutoff1, 'Marker'] = 0result = result.drop(['cutoff1', 'cutoff2'], axis=1)print(result)输出&nbsp; &nbsp;cond Array&nbsp; &nbsp; X&nbsp; &nbsp; &nbsp;Y&nbsp; Marker&nbsp; &nbsp; Area0&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;S&nbsp; 1.0&nbsp; 3.10&nbsp; &nbsp; 0.00&nbsp; &nbsp;3.0001&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;S&nbsp; 2.0&nbsp; 2.20&nbsp; &nbsp; 0.00&nbsp; &nbsp;2.0002&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;S&nbsp; 2.0&nbsp; 2.40&nbsp; &nbsp; 2.55&nbsp; &nbsp;2.4403&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;S&nbsp; 3.0&nbsp; 1.20&nbsp; &nbsp; 0.00&nbsp; &nbsp;1.2504&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;S&nbsp; 5.1&nbsp; 1.50&nbsp; &nbsp; 3.21&nbsp; &nbsp;0.1565&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; TT&nbsp; 3.0&nbsp; 2.10&nbsp; &nbsp; 0.00&nbsp; &nbsp;2.8806&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; TT&nbsp; 1.0&nbsp; 1.20&nbsp; &nbsp; 2.01&nbsp; &nbsp;1.3307&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; TT&nbsp; 4.0&nbsp; 1.50&nbsp; &nbsp; 0.00&nbsp; &nbsp;1.5308&nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; TT&nbsp; 7.3&nbsp; 1.33&nbsp; &nbsp; 3.20&nbsp; &nbsp;1.0009&nbsp; &nbsp; &nbsp;B&nbsp; &nbsp; &nbsp;S&nbsp; 3.2&nbsp; 1.60&nbsp; &nbsp; 0.00&nbsp; &nbsp;2.00010&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp;S&nbsp; 9.9&nbsp; 0.90&nbsp; &nbsp; 7.06&nbsp; &nbsp;6.90011&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp;S&nbsp; 3.2&nbsp; 0.78&nbsp; &nbsp; 8.10&nbsp; &nbsp;9.78012&nbsp; &nbsp; B&nbsp; &nbsp; TT&nbsp; 1.4&nbsp; 1.40&nbsp; &nbsp; 8.01&nbsp; &nbsp;2.40013&nbsp; &nbsp; B&nbsp; &nbsp; TT&nbsp; 5.5&nbsp; 1.30&nbsp; &nbsp; 9.10&nbsp; &nbsp;6.30014&nbsp; &nbsp; B&nbsp; &nbsp; TT&nbsp; 1.1&nbsp; 1.20&nbsp; &nbsp; 0.00&nbsp; 10.20015&nbsp; &nbsp; B&nbsp; &nbsp; TT&nbsp; 3.3&nbsp; 4.00&nbsp; &nbsp; 0.00&nbsp; 15.00016&nbsp; &nbsp; B&nbsp; &nbsp; TT&nbsp; 5.4&nbsp; 6.00&nbsp; &nbsp; 0.00&nbsp; 19.00017&nbsp; &nbsp; B&nbsp; &nbsp; SS&nbsp; 1.2&nbsp; 5.00&nbsp; &nbsp; 5.23&nbsp; 16.000请注意,此示例仅适用于cutoff1.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python