我有两个数据帧,
df1 = pd.DataFrame({'col_A':['AA','AA','AB','AB','AA','AA','AB','AB'],
'col_B':['BB','BB','BB','BB','BC','BC','BC','BC'],
'col_HIGH':[10,20,10,20,10,20,10,20],
'col_LOW':[0,10.0001,0,10.0001,0,10.0001,0,10.0001],
'col_D':[6,16,7,17,8,18,9,19]})
df2 = pd.DataFrame({'ID':['01','11','21','02','12','22','03','13','23','04','14','24','05','15','25','06','16','26','07','17','27','08','18','28'],
'col_A':['AA','AA','AA','AA','AA','AA','AB','AB','AB','AB','AB','AB','AA','AA','AA','AA','AA','AA','AB','AB','AB','AB','AB','AB'],
'col_B':['BB','BB','BB','BB','BB','BB','BB','BB','BB','BB','BB','BB','BC','BC','BC','BC','BC','BC','BC','BC','BC','BC','BC','BC',],
'col_C':[5,7,9,15,17,19,5,7,9,15,17,19,5,7,9,15,17,19,5,7,9,15,17,19]})
我想在df2中创建col_D并在df1中使用col_D的值基于多个条件进行填充,包括df1和df2中的col_A和col_B匹配,df2中的col_C值在df1中的col_HIGH和col_LOW范围内。最快的方法是什么?
答案 0 :(得分:0)
请尝试提供您要使用的条件。
dictionary = {'AA': 100, 'BB': 200, 'BC': 300}
df['new_column'] = df['old_column'].map(dictionary)
注意:您也可以在上方使用.apply()
if/else
条条件 df['new_column'] = ['value1' if x==100 else 'value2' for x in df['old column']]
答案 1 :(得分:0)
这是将join
与pd.IntervalIndex
结合使用的一种方式:
# Create a new dataframe with MultiIndex of col_A, col_B, and pd.IntervalIndex defined by
# LOW and HIGH columns
df1i = df1.set_index(['col_A', 'col_B', pd.IntervalIndex.from_arrays(df1['col_LOW'], df1['col_HIGH'], name='col_C')])
# Use join
df_out = df2.set_index(['col_A', 'col_B', 'col_C']).join(df1i).reset_index()
输出:
col_A col_B col_C ID col_HIGH col_LOW col_D
0 AA BB 5 01 10 0.0000 6
1 AA BB 7 11 10 0.0000 6
2 AA BB 9 21 10 0.0000 6
3 AA BB 15 02 20 10.0001 16
4 AA BB 17 12 20 10.0001 16
5 AA BB 19 22 20 10.0001 16
6 AB BB 5 03 10 0.0000 7
7 AB BB 7 13 10 0.0000 7
8 AB BB 9 23 10 0.0000 7
9 AB BB 15 04 20 10.0001 17
10 AB BB 17 14 20 10.0001 17
11 AB BB 19 24 20 10.0001 17
12 AA BC 5 05 10 0.0000 8
13 AA BC 7 15 10 0.0000 8
14 AA BC 9 25 10 0.0000 8
15 AA BC 15 06 20 10.0001 18
16 AA BC 17 16 20 10.0001 18
17 AA BC 19 26 20 10.0001 18
18 AB BC 5 07 10 0.0000 9
19 AB BC 7 17 10 0.0000 9
20 AB BC 9 27 10 0.0000 9
21 AB BC 15 08 20 10.0001 19
22 AB BC 17 18 20 10.0001 19
23 AB BC 19 28 20 10.0001 19
输出: