[英]Creating a variable based on the values of two other variables
I have a dataframe in pandas that include two variables: DEC and TYPE我在 Pandas 中有一个数据框,其中包含两个变量:DEC 和 TYPE
dec type
1 13
2 2
2 5
2 7
2 9
3 5
From these two variables, I would like to create other, binary, variables based on the values of these two variables.从这两个变量中,我想根据这两个变量的值创建其他二进制变量。
I haven't been able to find code to write exactly what I want, but in python-English, it would be something like:我一直无法找到完全写出我想要的代码,但在 python 英语中,它会是这样的:
df['new_variable'] = 1 if DEC == 1 & TYPE == 3 or 2 or 1
Please let me know if there is something I can include in my question to clarify what I am looking for.请让我知道是否可以在我的问题中包含一些内容来澄清我正在寻找的内容。
Update from answers:从答案更新:
A problem I am running into occurs because for each variable I need to run two lines of code (below) and when I run the second line it overruns the coding in the first line.我遇到的一个问题是因为对于每个变量,我需要运行两行代码(如下),当我运行第二行时,它超出了第一行中的编码。 How do I run both lines together (ie without the second line overrunning the first line)?我如何同时运行两行(即第二行不超过第一行)?
harrington_citations['gov_winner'] = np.where((harrington_citations['dec'] == 1) & harrington_citations['type'].isin([1,2,3,4,22]) , 1, 0)
harrington_citations['gov_winner'] = np.where((harrington_citations['dec'] == 2) & harrington_citations['type'].isin([1,5,9,13,18]), 1, 0)
Looks like you need .isin
for the second condition and return 1/0:看起来你需要.isin
作为第二个条件并返回 1/0:
df['new_variable'] = (df['dec'].eq(1) & df['type'].isin([3,2,1])).view('i1')
EDIT per comments, you should create 2 conditions with |
编辑每个评论,您应该使用|
创建 2 个条件comdition:条件:
c1 = (harrington_citations['dec'] == 1) & harrington_citations['type'].isin([1,2,3,4,22])
c2 = (harrington_citations['dec'] == 2) & harrington_citations['type'].isin([1,5,9,13,18])
harrington_citations['gov_winner'] = (c1|c2).view('i1')
将np.nan
替换为适合您的任何值:
df['new_variable'] = np.where((df['dec'] == 1) & df['type'].isin([1,2,3]), 1, np.nan)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.