I have a dataframe in pandas that include two variables: DEC and 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:
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:
df['new_variable'] = (df['dec'].eq(1) & df['type'].isin([3,2,1])).view('i1')
EDIT per comments, you should create 2 conditions with |
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.