使用 df1 中的值从 df2 中检索值,其中 df2 列和索引包含一系列值
Using values from df1 to retrieve values from df2 where df2 columns and index contain a range of values
我有一个数据框,其中包含姓名、performance_factor_1 和 performance_factor_2 等员工信息。
我有另一个数据框,我根据 performance_factor_1 和 performance_actor_2 获得报酬。
df1
Name pf1 pf2 pf3
Adam 14.6 8.9 59
Bob 13.2 9 75
Charlie 11.1 9.1 89
Dylan 14.6 9 97
Eric 11.1 8.8 105
Fedderick 12.5 9.2 69
df2
数据框 2 的行是 performance_factor_1,列是 performance_factor_2。
pf1 8.8-8.9 9.0-9.2 9.3-9.4 9.5-9.6 9.7-10
11.1 to 14 100 200 300 400 500
8.1 to 11 200 300 400 500 600
6.1 to 8 300 400 500 600 700
below 6 400 500 600 700 800
我想做的是向 df1 添加第三列 pay,如果 p3 高于 70,则如下所示:
df1
Name pf1 pf2 pay
Adam 14.6 8.9 200
Bob 13.2 9 400
Charlie 11.1 9.1 700
Dylan 14.6 9 300
Eric 11.1 8.8 400
Fedderick 12.5 9.2 700
我在之前的 post 中尝试过的是实际列出 14,13.9 --- 0.1,0 作为 pf1 索引和
列出 8.8、8.9 -- 10,然后使用查找来匹配确切的 pf1 和 pf2 值。但是,从长远来看,如果 df2 发生变化,这将是不适合的,因为在这种情况下,将有大量与更改 df2 的大多数值相关的手动工作。
这是我尝试在精确值匹配查找方法中使用的代码:
df_outer.reset_index(inplace=True)
df3 = indiv.rename(index= lambda x: int(x * 10),
columns= lambda x: int(float(x) * 10))
out= []
for row, col in zip(df_outer['TTR'].mul(10).astype(int), df_outer['CSAT (NSE)'].mul(10).astype(int)):
try:
out.append(df3.at[row, col] )
except KeyError:
out.append(np.nan)
df_outer['Pay'] = out
df_outer.loc[df_outer['# of Closed SRs']>=70, 'Pay_new'] = df_outer['Pay']
print (df_outer)
编辑:
所以最后,我有以下输出。但它使用的是 df2(old) 而我想使用 df2(new) 来获取我的输出
Name pf1 pf2 pf3 Pay
0 Adam 14.6 8.9 59 NaN
1 Bob 13.2 9.0 75 400
2 Charlie 11.1 9.1 89 700
3 Dylan 14.6 9.0 97 300
4 Eric 11.1 8.8 105 400
5 Fedderick 12.5 9.2 69 NaN
以前我的df2(old)是这样的
pf1 8.8 8.9 9 9.1 9.2
14.6 100 200 300 400 500
13.2 200 300 400 500 600
12.5 300 400 500 600 700
11.1 400 500 600 700 800
现在我希望我的 df2(new) 像这样
pf1 8.8-8.9 9.0-9.2 9.3-9.4 9.5-9.6 9.7-10
11.1 to 14 100 200 300 400 500
8.1 to 11 200 300 400 500 600
6.1 to 8 300 400 500 600 700
below 6 400 500 600 700 800
编辑 2:
我的 df 2 在 csv 中看起来像这样:
可以通过 IntervalIndex.from_tuples
in columns and index in df2
DataFrame and then change lookup with IntervalIndex.get_loc
:
创建 IntervalIndex
第一次测试:
print (df2.columns)
Index(['8.8-8.9', '9.0-9.2', '9.3-9.4', '9.5-9.6', '9.7-10'], dtype='object')
print (df2.index)
Index(['11.1 to 14', '8.1 to 11', '6.1 to 8', 'below 6'], dtype='object', name='pf1')
c = [(float(x[0]), float(x[1])) for x in df2.columns.str.split('-')]
i = [(0, float(x[0].split()[1])) if 'below' in x[0] else (float(x[0]), float(x[1]))
for x in df2.index.str.split(' to ')]
print (i)
[(11.1, 14.0), (8.1, 11.0), (6.1, 8.0), (0, 6.0)]
print (c)
[(8.8, 8.9), (9.0, 9.2), (9.3, 9.4), (9.5, 9.6), (9.7, 10.0)]
df2.columns = pd.IntervalIndex.from_tuples(c, closed='both')
df2.index = pd.IntervalIndex.from_tuples(i, closed='both')
print (df2)
[8.8, 8.9] [9.0, 9.2] [9.3, 9.4] [9.5, 9.6] [9.7, 10.0]
[11.1, 14.0] 100 200 300 400 500
[8.1, 11.0] 200 300 400 500 600
[6.1, 8.0] 300 400 500 600 700
[0.0, 6.0] 400 500 600 700 800
out= []
for row, col in zip(df1['pf1'], df1['pf2']):
try:
out.append(df2.iat[df2.index.get_loc(row), df2.columns.get_loc(col)])
except KeyError:
out.append(np.nan)
df1['Pay'] = out
print (df1)
Name pf1 pf2 pf3 Pay
0 Adam 14.6 8.9 59 NaN
1 Bob 13.2 9.0 75 200.0
2 Charlie 11.1 9.1 89 200.0
3 Dylan 14.6 9.0 97 NaN
4 Eric 11.1 8.8 105 100.0
5 Fedderick 12.5 9.2 69 200.0
我有一个数据框,其中包含姓名、performance_factor_1 和 performance_factor_2 等员工信息。
我有另一个数据框,我根据 performance_factor_1 和 performance_actor_2 获得报酬。
df1
Name pf1 pf2 pf3
Adam 14.6 8.9 59
Bob 13.2 9 75
Charlie 11.1 9.1 89
Dylan 14.6 9 97
Eric 11.1 8.8 105
Fedderick 12.5 9.2 69
df2 数据框 2 的行是 performance_factor_1,列是 performance_factor_2。
pf1 8.8-8.9 9.0-9.2 9.3-9.4 9.5-9.6 9.7-10
11.1 to 14 100 200 300 400 500
8.1 to 11 200 300 400 500 600
6.1 to 8 300 400 500 600 700
below 6 400 500 600 700 800
我想做的是向 df1 添加第三列 pay,如果 p3 高于 70,则如下所示: df1
Name pf1 pf2 pay
Adam 14.6 8.9 200
Bob 13.2 9 400
Charlie 11.1 9.1 700
Dylan 14.6 9 300
Eric 11.1 8.8 400
Fedderick 12.5 9.2 700
我在之前的 post
这是我尝试在精确值匹配查找方法中使用的代码:
df_outer.reset_index(inplace=True)
df3 = indiv.rename(index= lambda x: int(x * 10),
columns= lambda x: int(float(x) * 10))
out= []
for row, col in zip(df_outer['TTR'].mul(10).astype(int), df_outer['CSAT (NSE)'].mul(10).astype(int)):
try:
out.append(df3.at[row, col] )
except KeyError:
out.append(np.nan)
df_outer['Pay'] = out
df_outer.loc[df_outer['# of Closed SRs']>=70, 'Pay_new'] = df_outer['Pay']
print (df_outer)
编辑: 所以最后,我有以下输出。但它使用的是 df2(old) 而我想使用 df2(new) 来获取我的输出
Name pf1 pf2 pf3 Pay
0 Adam 14.6 8.9 59 NaN
1 Bob 13.2 9.0 75 400
2 Charlie 11.1 9.1 89 700
3 Dylan 14.6 9.0 97 300
4 Eric 11.1 8.8 105 400
5 Fedderick 12.5 9.2 69 NaN
以前我的df2(old)是这样的
pf1 8.8 8.9 9 9.1 9.2
14.6 100 200 300 400 500
13.2 200 300 400 500 600
12.5 300 400 500 600 700
11.1 400 500 600 700 800
现在我希望我的 df2(new) 像这样
pf1 8.8-8.9 9.0-9.2 9.3-9.4 9.5-9.6 9.7-10
11.1 to 14 100 200 300 400 500
8.1 to 11 200 300 400 500 600
6.1 to 8 300 400 500 600 700
below 6 400 500 600 700 800
编辑 2: 我的 df 2 在 csv 中看起来像这样:
可以通过 IntervalIndex.from_tuples
in columns and index in df2
DataFrame and then change lookup with IntervalIndex.get_loc
:
IntervalIndex
第一次测试:
print (df2.columns)
Index(['8.8-8.9', '9.0-9.2', '9.3-9.4', '9.5-9.6', '9.7-10'], dtype='object')
print (df2.index)
Index(['11.1 to 14', '8.1 to 11', '6.1 to 8', 'below 6'], dtype='object', name='pf1')
c = [(float(x[0]), float(x[1])) for x in df2.columns.str.split('-')]
i = [(0, float(x[0].split()[1])) if 'below' in x[0] else (float(x[0]), float(x[1]))
for x in df2.index.str.split(' to ')]
print (i)
[(11.1, 14.0), (8.1, 11.0), (6.1, 8.0), (0, 6.0)]
print (c)
[(8.8, 8.9), (9.0, 9.2), (9.3, 9.4), (9.5, 9.6), (9.7, 10.0)]
df2.columns = pd.IntervalIndex.from_tuples(c, closed='both')
df2.index = pd.IntervalIndex.from_tuples(i, closed='both')
print (df2)
[8.8, 8.9] [9.0, 9.2] [9.3, 9.4] [9.5, 9.6] [9.7, 10.0]
[11.1, 14.0] 100 200 300 400 500
[8.1, 11.0] 200 300 400 500 600
[6.1, 8.0] 300 400 500 600 700
[0.0, 6.0] 400 500 600 700 800
out= []
for row, col in zip(df1['pf1'], df1['pf2']):
try:
out.append(df2.iat[df2.index.get_loc(row), df2.columns.get_loc(col)])
except KeyError:
out.append(np.nan)
df1['Pay'] = out
print (df1)
Name pf1 pf2 pf3 Pay
0 Adam 14.6 8.9 59 NaN
1 Bob 13.2 9.0 75 200.0
2 Charlie 11.1 9.1 89 200.0
3 Dylan 14.6 9.0 97 NaN
4 Eric 11.1 8.8 105 100.0
5 Fedderick 12.5 9.2 69 200.0