Aug-04-2019, 11:19 AM
(This post was last modified: Aug-04-2019, 11:53 AM by ashishstats.)
Hi everyone
I am new to learn python but have experience in dealing data management and data analysis in Stata for last four years. While learning the codes in python (that already been developed in Stata) I got stuck in developing code, details are as follows:
In stata, I have a local macro called methods which contains 8 family planning method names separated with space: local methods "female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization". Further I have a string variable called method_discussed may contain no method name (blank), 1 to 8 method names separated with space from above macro based upon the responses from respondents from a survey i.e., method_discussed is multiple choice question. A sample of 5 observations is as follows where index 3 is blank (Assume respondent did not tell the any method name:
index method_discussed
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
.
.
.
.
so on.
While jumping to Python from Stata, I made a list,say, method_name=['female_condoms' 'emergency' 'male_condoms' 'pill' 'injectables' 'iud' 'male_sterilization' 'female_sterilization']. What I want to do is I want to generate 8 variables based on the name of items in list (method name actually) have data points as yes or no (1 or 0) if item of list is present in variable method_discussed. For eaxample
index method_discussed female_sterilization male_sterilization iud injectables antra_inj chhaya_pill pill male_condoms emergency female_condoms
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 1 1 1 0
0 0 1 0 1 0 0 0 0 0
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
Hi everyone
I am new to learn python 3.6, but, have experience in dealing data management and data analysis in Stata for last four years. While learning the codes in python (that already been developed in Stata) I got stuck in developing code, details are as follows:
In stata 15, I have a local macro called methods which contains 8 family planning method names separated with space: local methods "female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization". Further I have a string variable called method_discussed may contain no method name (blank), 1 to 8 method names separated with space from above macro based upon the responses from respondents from a survey i.e., method_discussed is multiple choice question. A sample of 5 observations is as follows where index 3 is blank (Assume respondent did not tell the any method name:
index method_discussed
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
.
.
.
.
so on.
While jumping to Python from Stata, I made a list,say, method_name=['female_condoms' 'emergency' 'male_condoms' 'pill' 'injectables' 'iud' 'male_sterilization' 'female_sterilization']. What I want to do is I want to generate 8 variables based on the name of items in list (method name actually) have data points as yes or no (1 or 0) if item of list is present in variable method_discussed. For eaxample, expected output should be like this
Data Input Expected output
index method_discussed female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization
1 iud male_condoms pill 0 0 1 1 0 1 0 0
2 male_condoms 0 0 1 0 0 0 0 0
3
4 female_sterilization male_sterilization 0 0 0 0 0 0 1 1
5 male_sterilization iud injectables 0 0 0 0 1 1 1 0
.
.
.
.
so on.
I am not able to understand how to proceed.
Anticipating help from your side
Ashish
I am new to learn python but have experience in dealing data management and data analysis in Stata for last four years. While learning the codes in python (that already been developed in Stata) I got stuck in developing code, details are as follows:
In stata, I have a local macro called methods which contains 8 family planning method names separated with space: local methods "female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization". Further I have a string variable called method_discussed may contain no method name (blank), 1 to 8 method names separated with space from above macro based upon the responses from respondents from a survey i.e., method_discussed is multiple choice question. A sample of 5 observations is as follows where index 3 is blank (Assume respondent did not tell the any method name:
index method_discussed
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
.
.
.
.
so on.
While jumping to Python from Stata, I made a list,say, method_name=['female_condoms' 'emergency' 'male_condoms' 'pill' 'injectables' 'iud' 'male_sterilization' 'female_sterilization']. What I want to do is I want to generate 8 variables based on the name of items in list (method name actually) have data points as yes or no (1 or 0) if item of list is present in variable method_discussed. For eaxample
index method_discussed female_sterilization male_sterilization iud injectables antra_inj chhaya_pill pill male_condoms emergency female_condoms
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 1 1 1 0
0 0 1 0 1 0 0 0 0 0
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
Hi everyone
I am new to learn python 3.6, but, have experience in dealing data management and data analysis in Stata for last four years. While learning the codes in python (that already been developed in Stata) I got stuck in developing code, details are as follows:
In stata 15, I have a local macro called methods which contains 8 family planning method names separated with space: local methods "female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization". Further I have a string variable called method_discussed may contain no method name (blank), 1 to 8 method names separated with space from above macro based upon the responses from respondents from a survey i.e., method_discussed is multiple choice question. A sample of 5 observations is as follows where index 3 is blank (Assume respondent did not tell the any method name:
index method_discussed
1 iud male_condoms pill
2 male_condoms
3
4 female_sterilization male_sterilization
5 male_sterilization iud injectables
.
.
.
.
so on.
While jumping to Python from Stata, I made a list,say, method_name=['female_condoms' 'emergency' 'male_condoms' 'pill' 'injectables' 'iud' 'male_sterilization' 'female_sterilization']. What I want to do is I want to generate 8 variables based on the name of items in list (method name actually) have data points as yes or no (1 or 0) if item of list is present in variable method_discussed. For eaxample, expected output should be like this
Data Input Expected output
index method_discussed female_condoms emergency male_condoms pill injectables iud male_sterilization female_sterilization
1 iud male_condoms pill 0 0 1 1 0 1 0 0
2 male_condoms 0 0 1 0 0 0 0 0
3
4 female_sterilization male_sterilization 0 0 0 0 0 0 1 1
5 male_sterilization iud injectables 0 0 0 0 1 1 1 0
.
.
.
.
so on.
I am not able to understand how to proceed.
Anticipating help from your side
Ashish