[英]Pandas: Perform boolean tests with values from table1 on table2
I have a question on Pandas. 我对熊猫有疑问。
I have table1 and table2 with the following values. 我的table1和table2具有以下值。 table2 basically contains a set of random values for each "name": table2基本上为每个“名称”包含一组随机值:
table1 = pd.DataFrame([{'name': 'A', 'value': 10},
{'name': 'B', 'value': 12},
{'name': 'C', 'value': 13}])
table2 = pd.DataFrame.from_dict({'name': ['A', 'B', 'C',
'A', 'B', 'C',
'A', 'B', 'C',
'A', 'B', 'C',
'A', 'B', 'C',
'A', 'B', 'C',
'A', 'B', 'C'],
'value': [np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30),
np.random.randint(0,20), np.random.randint(0,30), np.random.randint(0,30)]})
I want to: 我想要:
This can be generalized to N number of columns in table1, not just a "name" column. 可以将其概括为table1中N个列,而不仅仅是“名称”列。 I've googled high and low but I am only able to find basic boolean operators "eg table2[table2[value] <= 100]" but not by using a range of values from another table. 我已经在谷歌上搜索了高低,但是我只能找到基本的布尔运算符“例如table2 [table2 [value] <= 100]”,但是不能使用其他表中的一系列值。
I'm new to Python. 我是Python的新手。 Any help would be appreciated! 任何帮助,将不胜感激! Thank you! 谢谢!
This is one way. 这是一种方式。 Just map values from table1
to table2
, perform the comparison and convert the Boolean to your desired result. 只需将table1
值映射到table2
,执行比较并将Boolean转换为所需的结果。
table2['t1_value'] = table2['name'].map(table1.set_index('name')['value'])
table2['Greater/Less Than'] = (table2['value'] < table2['t1_value'])\
.map({True: 'Less Than', False: 'Greater Than'})
table2 = table2.drop('t1_value', 1)
# name value Greater/Less Than
# 0 A 7 Less Than
# 1 B 3 Less Than
# 2 C 24 Greater Than
# 3 A 9 Less Than
# 4 B 16 Greater Than
# 5 C 17 Greater Than
# 6 A 0 Less Than
# 7 B 5 Less Than
# 8 C 23 Greater Than
# 9 A 4 Less Than
# 10 B 11 Less Than
# 11 C 7 Less Than
# 12 A 11 Greater Than
# 13 B 8 Less Than
# 14 C 9 Less Than
# 15 A 0 Less Than
# 16 B 28 Greater Than
# 17 C 20 Greater Than
# 18 A 8 Less Than
# 19 B 23 Greater Than
# 20 C 2 Less Than
By using np.where
通过使用np.where
table2['Greater/Less Than']=np.where((table2.set_index('name')-table1.set_index('name')).gt(0),'Greater','Less')
table2
Out[31]:
name value Greater/Less Than
0 A 1 Less
1 B 27 Less
2 C 25 Less
3 A 7 Less
4 B 9 Greater
5 C 20 Less
6 A 3 Less
7 B 25 Greater
8 C 24 Less
9 A 9 Greater
10 B 2 Less
11 C 25 Greater
12 A 17 Less
13 B 16 Greater
14 C 3 Greater
15 A 8 Greater
16 B 1 Greater
17 C 13 Greater
18 A 3 Less
19 B 25 Less
20 C 29 Greater
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.