[英]Comparing/Mapping different series in different Dataframes
I have two data frames. 我有两个数据框。 Dataframe "A" which is the main dataframe has 3 columns "Number", "donation" and "Var1" . 作为主要数据帧的数据帧“ A”具有3列“ Number”,“ donation”和“ Var1”。 Dataframe B has 2 columns "Number" and "location". 数据框B具有2列“数字”和“位置”。 The "Number" column in DataFrame B is a subset of "Number" in A. What I would like to do is form a new column in DataFrame A - "NEW" which would map the values of numbers in both the column and if its present in DataFrame B would add value as 1 else all other values will be 0. DataFrame B中的“ Number”列是A中“ Number”的子集。我想做的是在DataFrame A中形成一个新列-“ NEW”,它将映射该列中的数字值以及是否映射出现在DataFrame B中的值将加1,否则所有其他值将为0。
>>>DFA
Number donation Var1
243 4 45
677 56 34
909 34 22
565 78 24
568 90 21
784 33 88
787 22 66
>>>DFB
Number location
909 PB
565 WB
784 AU
These are the two dataframes, I want the DFA with a new column which looks something like this. 这是两个数据框,我希望DFA带有一个新列,看起来像这样。
>>>DFA
Number donation Var1 NEW
243 4 45 0
677 56 34 0
909 34 22 1
565 78 24 1
568 90 21 0
784 33 88 1
787 22 66 0
This has a new column which has value as 1 if the Number was present in DFB if absent it gives 0. 如果DFB中存在数字,则该列具有一个新列,其值为1(如果不存在),则其值为0。
You could use the isin
method: 您可以使用isin
方法:
DFA['NEW'] = (DFA['Number'].isin(DFB['Number'])).astype(int)
For example, 例如,
import pandas as pd
DFA = pd.DataFrame({'Number': [243, 677, 909, 565, 568, 784, 787],
'Var1': [45, 34, 22, 24, 21, 88, 66],
'donation': [4, 56, 34, 78, 90, 33, 22]})
DFB = pd.DataFrame({'Number': [909, 565, 784], 'location': ['PB', 'WB', 'AU']})
DFA['NEW'] = (DFA['Number'].isin(DFB['Number'])).astype(int)
print(DFA)
yields 产量
Number Var1 donation NEW
0 243 45 4 0
1 677 34 56 0
2 909 22 34 1
3 565 24 78 1
4 568 21 90 0
5 784 88 33 1
6 787 66 22 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.