CHARLS_preprocess_main_all.py 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. import pandas as pd
  2. import numpy as np
  3. import pyreadstat
  4. from datetime import date
  5. from lunarcalendar import Converter, Lunar
  6. #统一列名
  7. def change_columns(df):
  8. df.columns = ["ID",'householdID','communityID','rgender', "birth_year", "birth_month", "ba003", "iyear", "imonth", "marital_status" , "education", 'province', 'city',"urban_nbs","Height", "Weight",
  9. "waist", "Systolic","Diastolic", "Sit_Stand_5x_yes", "Sit_Stand_5x_no",
  10. "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2",
  11. 'bl_wbc','bl_mcv','bl_plt','bl_bun','bl_glu','bl_crea','bl_cho', 'bl_tg', 'bl_hdl', 'bl_ldl','bl_crp',
  12. 'bl_hbalc','bl_ua', 'bl_hct', 'bl_hgb','bl_cysc',
  13. 'Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  14. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  15. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma',
  16. 'Physical_activity',
  17. 'Smoke','Drink', "Accident_Or_Injury","Fell_In_Last2Years", "Menarche_Year", "Menarche_Age", "Menopause_Year", "Menopause_Age",
  18. "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses"
  19. , "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  20. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  21. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  22. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  23. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  24. , "Interacted_With_Friends", "Played_Ma_jong", "Provided_help", "Sport", "Community_Related_Organization", "Charity_work", "Training_course", "Other", "None"
  25. , "Interacted_With_Friends_Fr", "Played_Ma_jong_Fr", "Provided_help_Fr", "Sport_Fr", "Community_Related_Organization_Fr", "Charity_work_Fr", "Training_course_Fr", "Other_Fr"
  26. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  27. , "Internet_Purpose_Chat", "Internet_Purpose_news","Internet_Purpose_videos","Internet_Purpose_games","Internet_Purpose_Financial","Internet_Purpose_Others"
  28. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  29. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  30. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency",
  31. 'Heart_attack_2_years', "Recurrent_Stroke",
  32. "Cognition_score", "Psychiatric_score","sleep_state", "ADL",
  33. 'Gas_Connection','Heating_Facility', 'Heating_Energy', 'Cooking_Fuel', "wave",
  34. ]
  35. # 2020年把帕金森和记忆病症分开,需要和以前对齐
  36. def process_row(row):
  37. da002_12_ = row['da003_12_']
  38. da002_13_ = row['da003_13_']
  39. if da002_12_ == 1 or da002_13_ == 1:
  40. return 1
  41. elif da002_12_ == 2 and da002_13_ == 2:
  42. return 2
  43. elif (da002_12_ == 2 and pd.isna(da002_13_)) or (pd.isna(da002_12_) and da002_13_ == 2):
  44. return 2
  45. elif pd.isna(da002_12_) and pd.isna(da002_13_):
  46. return np.nan
  47. else:
  48. return np.nan # 预防万一,其余情况下设为NA
  49. def update_da051(value):
  50. if value == 1:
  51. return 3
  52. elif value == 3:
  53. return 1
  54. else:
  55. return value
  56. if __name__ == "__main__":
  57. # 2011年
  58. year = "2011"
  59. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/demographic_background.dta")
  60. psu, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/psu.dta", encoding='gbk')
  61. biomarkers, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/biomarkers.dta")
  62. blood, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Blood_20140429.dta")
  63. health_status, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/health_status_and_functioning.dta")
  64. health_care, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/health_care_and_insurance.dta")
  65. exp_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/exp_income_wealth.dta")
  66. weight, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/weight.dta")
  67. houseing, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/housing_characteristics.dta")
  68. community, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/community.dta")
  69. family_information, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/family_information.dta")
  70. family_transfer, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/family_transfer.dta")
  71. household_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/household_income.dta")
  72. household_roster, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/household_roster.dta")
  73. individual_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/individual_income.dta")
  74. interviewer_observation, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/interviewer_observation.dta")
  75. work_retirement_and_pension, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/work_retirement_and_pension.dta")
  76. #性别#年龄#居住地#婚姻状况
  77. # 1 married or partnered
  78. # 0 other marital status (separated, divorced, unmarried, or widowed)
  79. demo["marital_status"] = demo.apply(lambda x : 1 if x["be001"]==1 or x["be001"]==2 or x["be002"]==1 else 0 if x["be001"] in [3,4,5,6] else np.nan, axis=1)
  80. #教育
  81. # 0 below high school
  82. # 1 high school
  83. # 2 college or above
  84. demo["education"] = demo["bd001"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  85. #获取随访时间
  86. demo = pd.merge(demo, weight[["ID", "iyear", "imonth"]], on = "ID", how="left")
  87. data_2011 = demo[['ID','householdID', 'communityID','rgender','ba002_1', 'ba002_2','ba003',"iyear", "imonth" ,'marital_status', 'education']]
  88. #居住地
  89. # 0 农村
  90. # 1 城市
  91. data_2011 = pd.merge(data_2011, psu[['communityID', 'province', 'city', 'urban_nbs']], on = "communityID", how="left")
  92. #身高#体重#收缩压#舒张压
  93. biomarkers["qi002"] = biomarkers["qi002"].apply(lambda x : np.nan if x >210 else x)
  94. biomarkers["ql002"] = biomarkers["ql002"].apply(lambda x : np.nan if x >150 else x)
  95. #腰围
  96. biomarkers['waist'] = biomarkers["qm002"].apply(lambda x : np.nan if x >210 else x)
  97. #血压测量后两次的平均
  98. biomarkers["qa007"] = biomarkers["qa007"].apply(lambda x : np.nan if x >300 else x)
  99. biomarkers["qa011"] = biomarkers["qa011"].apply(lambda x : np.nan if x >300 else x)
  100. biomarkers["qa008"] = biomarkers["qa008"].apply(lambda x : np.nan if x >150 else x)
  101. biomarkers["qa012"] = biomarkers["qa012"].apply(lambda x : np.nan if x >150 else x)
  102. biomarkers["Systolic"] = (biomarkers["qa007"] + biomarkers["qa011"]) /2
  103. biomarkers["Diastolic"] = (biomarkers["qa008"] + biomarkers["qa012"]) /2
  104. #受试者可以在不用手臂支撑的情况下按其平时的节奏连续起立坐下五次吗
  105. # yes
  106. biomarkers["Sit_Stand_5x_yes"] = biomarkers["qh003"]
  107. # no
  108. biomarkers["Sit_Stand_5x_no"] = biomarkers["qh004"]
  109. # times
  110. biomarkers["Sit_Stand_5x_times"] = biomarkers["qh005"]
  111. # 步行速度时间
  112. biomarkers["Walking_Speed_Time_1"] = biomarkers["qg002"]
  113. biomarkers["Walking_Speed_Time_2"] = biomarkers["qg003"]
  114. biomarkers_select = biomarkers[['ID','householdID', 'communityID','qi002','ql002', "waist",'Systolic','Diastolic', "Sit_Stand_5x_yes", "Sit_Stand_5x_no",
  115. "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2"]]
  116. data_2011 = pd.merge(data_2011, biomarkers_select, on = ["ID", "householdID", "communityID"], how="left")
  117. #白细胞(WBC),平均红血球容积MCV,血小板,血尿素氮bun,葡萄糖glu,血肌酐crea,总胆固醇cho,甘油三酯tg,高密度脂蛋白HDL,低密度脂蛋白胆固醇LDL,C反应蛋白CRP
  118. #糖化血红蛋白hba1c,尿酸ua,血细胞比容Hematocrit,血红蛋白hgb,胱抑素C
  119. blood = blood.loc[:, blood.columns.difference(["bloodweight", "qc1_va003"])]
  120. data_2011 = pd.merge(data_2011, blood, on = ["ID"], how="left")
  121. # 慢性病:
  122. # (1) Hypertension 高血压病
  123. # (2) Dyslipidemia (elevation of low density lipoprotein, triglycerides (TGs),and total cholesterol, or a low high density lipoprotein level)血脂异常(包括低密度脂蛋白、甘油三酯、总胆固醇的升高或(和)高密度脂蛋白的下降)
  124. # (3) Diabetes or high blood sugar糖尿病或血糖升高(包括糖耐量异常和空腹血糖升高)
  125. # (4) Cancer or malignant tumor (excluding minor skin cancers) 癌症等恶性肿瘤(不包括轻度皮肤癌)
  126. # (5) Chronic lung diseases, such as chronic bronchitis , emphysema ( excluding tumors, or cancer) 慢性肺部疾患如慢性支气管炎或肺气肿、肺心病(不包括肿瘤或癌)
  127. # (6) Liver disease (except fatty liver, tumors, and cancer) 肝脏疾病
  128. # (除脂肪肝、肿瘤或癌外)
  129. # (7) Heart attack, coronary heart disease, angina, congestive heart failure, or other heart problems 心脏病(如心肌梗塞、冠心病、心绞痛、充血性心力衰竭和其他心脏疾病)
  130. # (8) Stroke 中风
  131. # (9) Kidney disease (except for tumor or cancer) 肾脏疾病(不包括肿瘤或癌)
  132. # (10) Stomach or other digestive disease (except for tumor or cancer) 胃部疾病或消化系统疾病(不包括肿瘤或癌)
  133. # (11) Emotional, nervous, or psychiatric problems 情感及精神方面问题
  134. # (12) Memory-related disease 与记忆相关的疾病 (如老年痴呆症、脑萎缩、帕金森症)
  135. # (13) Arthritis or rheumatism 关节炎或风湿病
  136. # (14) Asthma 哮喘
  137. # 体力活动
  138. # 2 vigorous (vigorous activity more than once a week)
  139. # 1 moderate (moderate activity more than once a week)
  140. # 0 inactive (the rest)
  141. health_status["Physical_activity"] = health_status.apply(lambda x : 2 if x["da051_1_"]==1 else
  142. 1 if x["da051_2_"]==1 else
  143. 0 if x["da051_3_"] == 1 or (x["da051_1_"]==2 and x["da051_2_"]==2 and x["da051_3_"] == 2)
  144. else np.nan ,axis=1)
  145. # 抽烟
  146. # 1 抽过烟
  147. # 0 没有抽过烟
  148. health_status["Smoke"] = health_status["da059"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  149. # 喝酒
  150. # 1 喝过酒
  151. # 0 没有喝过酒
  152. health_status["Drink"] = health_status.apply(lambda x : 1 if x["da067"] ==1 or x["da067"] ==2 else
  153. 0 if x["da069"] == 1 else
  154. 1 if x["da069"] == 2 or x["da069"] == 3 else np.nan, axis=1)
  155. # 您是否经历过交通事故,或任何的重大意外伤害,并接受了治疗?
  156. # 1 是
  157. # 0 否
  158. health_status['Accident_Or_Injury']=health_status["da021"].apply(lambda x : 1 if x ==1 else
  159. 0 if x == 2 else np.nan)
  160. # 过去两年有没有摔倒?
  161. # 1 是
  162. # 0 否
  163. health_status['Fell_In_Last2Years']=health_status["da023"].apply(lambda x : 1 if x ==1 else
  164. 0 if x == 2 else np.nan)
  165. # 您什么时候开始来月经的?(year/age)
  166. health_status['Menarche_Year']=health_status["da026_1"]
  167. health_status['Menarche_Age']=health_status["da026_2"]
  168. # 您什么时候开始绝经的?
  169. health_status['Menopause_Year']=health_status["da028_1"]
  170. health_status['Menopause_Age']=health_status["da028_2"]
  171. # 第一次诊断出您有前列腺疾病是在什么时候?
  172. health_status['Prostate_Issue_Year']=health_status["da030_1"]
  173. health_status['Prostate_Issue_Age']=health_status["da030_2"]
  174. # 是否戴眼镜(包括矫正视力镜片)?
  175. # 1 是
  176. # 0 否
  177. # 2 失明
  178. # 3 偶尔
  179. health_status['Wear_Glasses']=health_status["da032"].apply(lambda x : 1 if x == 1 else 2 if x ==2 else 0 if x == 3 else np.nan)
  180. # 过去一个月内,您平均每天晚上真正睡着的时间大约是几小时?(可能短于您在床上躺着的时间)
  181. health_status['Average_Sleep_Hours']=health_status["da049"]
  182. # 过去一个月内,您通常午睡多长时间?分钟
  183. health_status['Average_Nap_Minutes']=health_status["da050"]
  184. # 您通常每周有没有至少持续做激烈活动十分钟?
  185. health_status['Vigorous_Activity_10Min']=health_status["da051_1_"]
  186. # 您通常每周有没有至少持续做中等强度的体力活动十分钟?
  187. health_status['Moderate_Effort_10Min']=health_status["da051_2_"]
  188. # 您通常每周有没有至少持续走路十分钟?
  189. health_status['Walking_10Min']=health_status["da051_3_"]
  190. # 您通常每周有多少天做[激烈活动]至少十分钟?
  191. health_status['Vigorous_Activity_Days']=health_status["da052_1_"]
  192. # 您通常每周有多少天做[中等强度的体力活动]至少十分钟?
  193. health_status['Moderate_Effort_Days']=health_status["da052_2_"]
  194. # 您通常每周有多少天做[走路]至少十分钟?
  195. health_status['Walking_Days']=health_status["da052_3_"]
  196. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 2小时
  197. health_status['Vigorous_Activity_2Hours_PerDay']=health_status["da053_1_"]
  198. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 2小时
  199. health_status['Moderate_Effort_2Hours_PerDay']=health_status["da053_2_"]
  200. # 在做[走路]的这些天里,您一天花多少时间做[走路] 2小时
  201. health_status['Walking_2Hours_PerDay']=health_status["da053_3_"]
  202. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 30分钟
  203. health_status['Vigorous_Activity_30Min_PerDay']=health_status["da054_1_"]
  204. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 30分钟
  205. health_status['Moderate_Effort_30Min_PerDay']=health_status["da054_2_"]
  206. # 在做[走路]的这些天里,您一天花多少时间做[走路] 30分钟
  207. health_status['Walking_30Min_PerDay']=health_status["da054_3_"]
  208. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 4小时
  209. health_status['Vigorous_Activity_4Hours_PerDay']=health_status["da055_1_"]
  210. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 4小时
  211. health_status['Moderate_Effort_4Hours_PerDay']=health_status["da055_2_"]
  212. # 在做[走路]的这些天里,您一天花多少时间做[走路] 4小时
  213. health_status['Walking_4Hours_PerDay']=health_status["da055_3_"]
  214. # 活动的原因
  215. # 1 工作需要
  216. # 2 娱乐
  217. # 3 体育锻炼
  218. # 4 其他
  219. health_status[["Reason_For_Vigorous_Activity"]]= np.nan
  220. health_status[["Reason_For_Moderate_Effort"]]= np.nan
  221. health_status[["Reason_For_Walking"]]= np.nan
  222. # 过去一个月是否进行了下列社交活动?
  223. # (1) 串门、跟朋友交往
  224. # (2) 打麻将、下棋、打牌、去社区活动室
  225. # (3) 无偿向与您不住在一起的亲人、朋友或者邻居提供帮助
  226. # (4) 去公园或者其他场所跳舞、健身、练气功等
  227. # (5) 参加社团组织活动
  228. # (6) 志愿者活动或者慈善活动/无偿照顾与您不住在一起的病人或残疾人
  229. # (7) 上学或者参加培训课程
  230. # (8)其他
  231. # (9) 以上均没有
  232. health_status["da056s1"] = health_status.apply(lambda x: 1 if x["da056s1"]==1 else 0, axis=1)
  233. health_status["da056s2"] = health_status.apply(lambda x: 2 if x["da056s2"]==2 else 0, axis=1)
  234. health_status["da056s3"] = health_status.apply(lambda x: 3 if x["da056s3"]==3 else 0, axis=1)
  235. health_status["da056s4"] = health_status.apply(lambda x: 4 if x["da056s4"]==4 else 0, axis=1)
  236. health_status["da056s5"] = health_status.apply(lambda x: 5 if x["da056s5"]==5 else 0, axis=1)
  237. health_status["da056s6"] = health_status.apply(lambda x: 6 if x["da056s6"]==6 or x["da056s7"]==7 else 0, axis=1)
  238. health_status["da056s7"] = health_status.apply(lambda x: 7 if x["da056s8"]==8 else 0, axis=1)
  239. health_status["da056s8"] = health_status.apply(lambda x: 8 if x["da056s9"]==9 or x["da056s10"]==10 or x["da056s11"]==11 else 0, axis=1)
  240. health_status["da056s9"] = health_status.apply(lambda x: 7 if x["da056s12"]==12 else 0, axis=1)
  241. # 过去一个月的活动频率
  242. # (1) Almost daily 差不多每天
  243. # (2) Almost every week 差不多每周
  244. # (3) Not regularly 不经常
  245. health_status["da057_6_"] = health_status.apply(lambda x: 1 if x["da057_6_"]==1 or x["da057_7_"]==1 else 2 if x["da057_6_"]==2 or x["da057_7_"]==2 else 3 if x["da057_6_"]==3 or x["da057_7_"]==3 else np.nan, axis=1)
  246. health_status["da057_7_"] = health_status["da057_8_"]
  247. health_status["da057_8_"] = health_status.apply(lambda x: 1 if x["da057_9_"]==1 or x["da057_10_"]==1 or x["da057_11_"]==1
  248. else 2 if x["da057_9_"]==2 or x["da057_10_"]==2 or x["da057_11_"]==2
  249. else 3 if x["da057_9_"]==3 or x["da057_10_"]==3 or x["da057_11_"]==3
  250. else np.nan, axis=1)
  251. # 过去一个月,您是否上网?
  252. # 1 是
  253. # 0 否
  254. health_status["Internet_Usage_LastMonth"] = health_status["da056s10"].apply(lambda x : 1 if x==10 else 0)
  255. # 使用以下哪些工具上网?
  256. health_status[["Internet_Tools_Desktop_computer"]] = np.nan
  257. health_status[["Internet_Tools_Laptop_computer"]] = np.nan
  258. health_status[["Internet_Tools_Tablet_computer"]] = np.nan
  259. health_status[["Internet_Tools_Cellphone"]] = np.nan
  260. health_status[["Internet_Tools_Other"]] = np.nan
  261. # 上网一般做什么?
  262. health_status[["Internet_Purpose_Chat"]] = np.nan
  263. health_status[["Internet_Purpose_news"]] = np.nan
  264. health_status[["Internet_Purpose_videos"]] = np.nan
  265. health_status[["Internet_Purpose_games"]] = np.nan
  266. health_status[["Internet_Purpose_Financial"]] = np.nan
  267. health_status[["Internet_Purpose_Others"]] = np.nan
  268. # 是否会用手机支付
  269. health_status[["Mobile_Payment"]] = np.nan
  270. # 是否使用微信?
  271. health_status[["Wechat_Usage"]] = np.nan
  272. # 发不发微信朋友圈?
  273. health_status[["Post_Moments"]] = np.nan
  274. # 现在还在吸烟还是戒烟了?
  275. # 1 仍然抽烟 Skip DA062 请跳过DA062
  276. # 2 戒烟
  277. health_status['Current_Smoking_Status']=health_status["da061"]
  278. # 吸烟时,一般抽什么烟?
  279. # (1) Smoking a pipe 用烟管吸烟(烟袋、旱烟)
  280. # (2) Smoking self-rolled cigarettes 自己卷烟抽
  281. # (3) Filtered cigarette带滤咀香烟
  282. # (4) Unfiltered cigarette不带滤咀香烟
  283. # (5) Cigar雪茄
  284. # (6) Water cigarettes 水烟
  285. health_status.loc[health_status['da060'] == 1, 'Smoking_Type_pipe'] = 1
  286. health_status.loc[health_status['da060'] == 2, 'Smoking_Type_rolled'] = 2
  287. health_status.loc[health_status['da060'] == 3, 'Smoking_Type_Filtered'] = 3
  288. health_status.loc[health_status['da060'] == 4, 'Smoking_Type_Unfiltered'] = 4
  289. health_status.loc[health_status['da060'] == 5, 'Smoking_Type_Cigar'] = 5
  290. health_status.loc[health_status['da060'] == 6, 'Smoking_Type_Water'] = 6
  291. # 现在/戒烟前平均一天抽多少支香烟?
  292. health_status['Daily_Cigarette_Count']=health_status["da063"]
  293. # 在过去的一年, 喝酒吗
  294. # (1) Drink more than once a month. 喝酒,每月超过一次
  295. # (2) Drink but less than once a month 喝酒,但每月少于一次
  296. # (3) None of these 什么都不喝
  297. health_status['Drink_PastYear']=health_status["da067"]
  298. # 过去一年内 平均一个月喝几次酒
  299. # (1)Once a month 每月一次
  300. # (2)2-3 times a month 每月2-3次
  301. # (3)Once a week 每周一次
  302. # (4)2-3 times a week 每周2-3次
  303. # (5)4-6 times a week 每周4-6次
  304. # (6)Once a day 每天一次
  305. # (7)Twice a day 一天两次
  306. # (8)More than twice a day 一天超过两次
  307. health_status['Drink_Monthly_Frequency']=health_status.apply(lambda x : 8 if x["da072"] ==8 or x["da074"] ==8 or x["da076"] ==8 else
  308. 7 if x["da072"] ==7 or x["da074"] ==7 or x["da076"] ==7 else
  309. 6 if x["da072"] ==6 or x["da074"] ==6 or x["da076"] ==6 else
  310. 5 if x["da072"] ==5 or x["da074"] ==5 or x["da076"] ==5 else
  311. 4 if x["da072"] ==4 or x["da074"] ==4 or x["da076"] ==4 else
  312. 3 if x["da072"] ==3 or x["da074"] ==3 or x["da076"] ==3 else
  313. 2 if x["da072"] ==2 or x["da074"] ==2 or x["da076"] ==2 else
  314. 1 if x["da072"] ==1 or x["da074"] ==1 or x["da076"] ==1 else np.nan, axis=1)
  315. health_status_select = health_status[['ID','householdID', 'communityID', 'da007_1_', 'da007_2_','da007_3_'
  316. ,'da007_4_','da007_5_','da007_6_','da007_7_','da007_8_','da007_9_','da007_10_','da007_11_'
  317. ,'da007_12_','da007_13_','da007_14_', "Physical_activity", "Smoke", "Drink"
  318. , "Accident_Or_Injury", "Fell_In_Last2Years", "Menarche_Year", "Menarche_Age"
  319. , "Menopause_Year", "Menopause_Age", "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses"
  320. , "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  321. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  322. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  323. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  324. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  325. , "da056s1", "da056s2", "da056s3", "da056s4", "da056s5", "da056s6", "da056s7", "da056s8", "da056s9"
  326. , "da057_1_" , "da057_2_", "da057_3_", "da057_4_", "da057_5_", "da057_6_", "da057_7_", "da057_8_"
  327. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  328. , "Internet_Purpose_Chat", "Internet_Purpose_news","Internet_Purpose_videos","Internet_Purpose_games","Internet_Purpose_Financial","Internet_Purpose_Others"
  329. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  330. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  331. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency"]]
  332. data_2011 = pd.merge(data_2011, health_status_select, on = ["ID", 'householdID', 'communityID'], how="left")
  333. # 自上次访问以来的两年内,您是否发作过心脏病?
  334. # 1 是
  335. # 0 否
  336. data_2011[['Heart_attack_2_years']]=np.nan
  337. # 自上次访问以来,是否有医生诊断您中风复发?
  338. # 1 是
  339. # 0 否
  340. data_2011[['Recurrent_Stroke']]=np.nan
  341. #计算认知功能得分,分成三部分:电话问卷9分,词语回忆20分、画图1分
  342. health_status["dc001s1_score"] = health_status["dc001s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  343. health_status["dc001s2_score"] = health_status["dc001s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  344. health_status["dc001s3_score"] = health_status["dc001s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  345. health_status["dc002_score"] = health_status["dc002"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  346. # health_status["dc003_score"] = health_status["dc003"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  347. health_status["dc019_score"] = health_status["dc019"].apply(lambda x : 1 if x==93 else 0 if pd.isna(x) else 0)
  348. health_status["dc020_score"] = health_status["dc020"].apply(lambda x : 1 if x==86 else 0 if pd.isna(x) else 0)
  349. health_status["dc021_score"] = health_status["dc021"].apply(lambda x : 1 if x==79 else 0 if pd.isna(x) else 0)
  350. health_status["dc022_score"] = health_status["dc022"].apply(lambda x : 1 if x==72 else 0 if pd.isna(x) else 0)
  351. health_status["dc023_score"] = health_status["dc023"].apply(lambda x : 1 if x==65 else 0 if pd.isna(x) else 0)
  352. #词语记忆
  353. health_status["dc006s1_score"] = health_status["dc006s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  354. health_status["dc006s2_score"] = health_status["dc006s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  355. health_status["dc006s3_score"] = health_status["dc006s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  356. health_status["dc006s4_score"] = health_status["dc006s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  357. health_status["dc006s5_score"] = health_status["dc006s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  358. health_status["dc006s6_score"] = health_status["dc006s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  359. health_status["dc006s7_score"] = health_status["dc006s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  360. health_status["dc006s8_score"] = health_status["dc006s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  361. health_status["dc006s9_score"] = health_status["dc006s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  362. health_status["dc006s10_score"] = health_status["dc006s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  363. # health_status["dc006s11_score"] = health_status["dc006s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  364. health_status["dc027s1_score"] = health_status["dc027s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  365. health_status["dc027s2_score"] = health_status["dc027s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  366. health_status["dc027s3_score"] = health_status["dc027s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  367. health_status["dc027s4_score"] = health_status["dc027s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  368. health_status["dc027s5_score"] = health_status["dc027s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  369. health_status["dc027s6_score"] = health_status["dc027s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  370. health_status["dc027s7_score"] = health_status["dc027s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  371. health_status["dc027s8_score"] = health_status["dc027s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  372. health_status["dc027s9_score"] = health_status["dc027s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  373. health_status["dc027s10_score"] = health_status["dc027s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  374. # health_status["dc027s11_score"] = health_status["dc027s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  375. #画图
  376. health_status["draw_score"] = health_status["dc025"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  377. data_2011["Cognition_score"] = health_status["dc001s1_score"] + health_status["dc001s2_score"] + \
  378. health_status["dc001s3_score"] + health_status["dc002_score"]+ \
  379. health_status["dc019_score"]+ health_status["dc020_score"] + health_status["dc021_score"]+ \
  380. health_status["dc022_score"]+ health_status["dc023_score"] + health_status["dc006s1_score"] + \
  381. health_status["dc006s2_score"] + health_status["dc006s3_score"] + health_status["dc006s4_score"] + \
  382. health_status["dc006s5_score"] + health_status["dc006s6_score"] + health_status["dc006s7_score"] + \
  383. health_status["dc006s8_score"] + health_status["dc006s9_score"] + health_status["dc006s10_score"] + \
  384. health_status["dc027s1_score"]+ health_status["dc027s2_score"]+ \
  385. health_status["dc027s3_score"]+ health_status["dc027s4_score"]+ health_status["dc027s5_score"]+ \
  386. health_status["dc027s6_score"]+ health_status["dc027s7_score"]+ health_status["dc027s8_score"]+ \
  387. health_status["dc027s9_score"]+health_status["dc027s10_score"]+\
  388. health_status["draw_score"]
  389. #心理得分
  390. health_status["dc009_score"] = health_status["dc009"]-1
  391. health_status["dc010_score"] = health_status["dc010"]-1
  392. health_status["dc011_score"] = health_status["dc011"]-1
  393. health_status["dc012_score"] = health_status["dc012"]-1
  394. health_status["dc013_score"] = 4 - health_status["dc013"]
  395. health_status["dc014_score"] = health_status["dc014"]-1
  396. health_status["dc015_score"] = health_status["dc015"]-1
  397. health_status["dc016_score"] = 4 - health_status["dc016"]
  398. health_status["dc017_score"] = health_status["dc017"]-1
  399. health_status["dc018_score"] = health_status["dc018"]-1
  400. data_2011["psychiatric_score"] = health_status["dc009_score"] + health_status["dc010_score"] + health_status["dc011_score"] + \
  401. health_status["dc012_score"] + health_status["dc013_score"] + health_status["dc014_score"] + health_status["dc015_score"] + \
  402. health_status["dc016_score"] + health_status["dc017_score"] + health_status["dc018_score"]
  403. #睡眠状态
  404. # (1)Rarely or none of the time (<1 day) 很少或者根本没有(<1天)
  405. # (2)Some or a little of the time (1-2 days) 不太多(1-2天)
  406. # (3)Occasionally or a moderate amount of the time (3-4 days) 有时或者说有一半的时间(3-4天)
  407. # (4)Most or all of the time (5-7 days) 大多数的时间(5-7天)
  408. data_2011["sleep_state"] = health_status['dc015']
  409. #ADL
  410. health_status["db010_score"] = health_status["db010"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  411. health_status["db011_score"] = health_status["db011"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  412. health_status["db012_score"] = health_status["db012"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  413. health_status["db013_score"] = health_status["db013"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  414. health_status["db014_score"] = health_status["db014"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  415. health_status["db015_score"] = health_status["db015"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  416. data_2011["ADL"] = health_status["db010_score"] + health_status["db011_score"] + health_status["db012_score"] + health_status["db013_score"] + \
  417. health_status["db014_score"] + health_status["db015_score"]
  418. # 是否有管道煤气或天然气?
  419. houseing["Gas_Connection"] = houseing["i019"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  420. # 是否带供暖设施(不包括土暖气和可制暖的空调)?
  421. houseing["Heating_Facility"] = houseing["i020"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  422. # 供暖所用的主要能源是什么?
  423. # (1)Solar 太阳能
  424. # (2)Coal 煤炭、蜂窝煤
  425. # (3)Natural gas 管道天然气或煤气
  426. # (4)Liquefied Petroleum Gas 液化石油气
  427. # (5)Electric 电
  428. # (6)Crop residue/Wood buring 秸秆、柴火
  429. # (7)Other 其他
  430. houseing["Heating_Energy"] = houseing["i021"]
  431. # 做饭用的主要燃料是什么?
  432. # (1)Coal 煤炭、蜂窝煤
  433. # (2)Natural gas 管道天然气或煤气
  434. # (3)Marsh gas 沼气
  435. # (4)Liquefied Petroleum Gas 液化石油气
  436. # (5)Electric 电
  437. # (6)crop residue/Wood burning 秸秆、柴火
  438. # (7)other 其他
  439. houseing["Cooking_Fuel"] = houseing["i022"]
  440. houseing_select = houseing[['householdID', 'communityID','Gas_Connection',
  441. 'Heating_Facility', 'Heating_Energy', 'Cooking_Fuel']]
  442. data_2011 = pd.merge(data_2011, houseing_select, on = ['householdID', 'communityID'], how="left")
  443. data_2011["wave"] = year
  444. change_columns(data_2011)
  445. # 将全部数据进行合并
  446. data_2011 = pd.merge(data_2011, demo, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_demo'))
  447. data_2011 = pd.merge(data_2011, psu[["communityID", "areatype"]], on = ['communityID'], how="left", suffixes=('', '_psu'))
  448. data_2011 = pd.merge(data_2011, biomarkers, on = ["ID", "householdID", "communityID"], how="left", suffixes=('', '_bio'))
  449. data_2011 = pd.merge(data_2011, health_status, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_statush'))
  450. data_2011 = pd.merge(data_2011, health_care, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_careh'))
  451. data_2011 = pd.merge(data_2011, exp_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomee'))
  452. data_2011 = pd.merge(data_2011, weight, on = ["ID", 'householdID', 'communityID', "iyear", "imonth"], how="left", suffixes=('', '_weightw'))
  453. data_2011 = pd.merge(data_2011, houseing, on = ['householdID', 'communityID'], how="left", suffixes=('', '_houseing'))
  454. # data_2011 = pd.merge(data_2011, community, on = ['communityID','sub_commuID'], how="left", suffixes=('', '_community'))
  455. data_2011 = pd.merge(data_2011, family_information, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_information'))
  456. data_2011 = pd.merge(data_2011, family_transfer, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_transfer'))
  457. data_2011 = pd.merge(data_2011, household_income, on = ['householdID', 'communityID'], how="left", suffixes=('', '_incomeh'))
  458. data_2011 = pd.merge(data_2011, household_roster, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_rosterh'))
  459. data_2011 = pd.merge(data_2011, individual_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomei'))
  460. data_2011 = pd.merge(data_2011, interviewer_observation, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_observation'))
  461. data_2011 = pd.merge(data_2011, work_retirement_and_pension, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_pension'))
  462. # 2011年的ID和其他年份有一点区别,倒数第三位加0
  463. data_2011["ID"] = data_2011["ID"].apply(lambda x : x[:-2] + '0' + x[-2:] if len(str(x)) >= 3 else x)
  464. #修改地区名称
  465. #省份、城市名称和污染物数据格式对齐
  466. #海东地区->海东市
  467. data_2011['city'] = data_2011['city'].replace('海东地区', '海东市')
  468. #北京 -> 北京市
  469. data_2011['city'] = data_2011['city'].replace('北京', '北京市')
  470. data_2011['province'] = data_2011['province'].replace('北京', '北京市')
  471. #哈尔滨 -> 哈尔滨市
  472. data_2011['city'] = data_2011['city'].replace('哈尔滨', '哈尔滨市')
  473. #天津 -> 天津市
  474. data_2011['city'] = data_2011['city'].replace('天津', '天津市')
  475. data_2011['province'] = data_2011['province'].replace('天津', '天津市')
  476. #广西省 -> 广西壮族自治区
  477. data_2011['province'] = data_2011['province'].replace('广西省', '广西壮族自治区')
  478. #巢湖市 -> 合肥市
  479. data_2011['city'] = data_2011['city'].replace('巢湖市', '合肥市')
  480. #襄樊市->襄阳市
  481. data_2011['city'] = data_2011['city'].replace('襄樊市', '襄阳市')
  482. data_2011.to_csv("2011_all_charls.csv", index=False)
  483. print("2011 complete")
  484. # 2013年
  485. year = "2013"
  486. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Demographic_Background.dta")
  487. psu, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/PSU.dta", encoding='gbk')
  488. biomarkers, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Biomarker.dta")
  489. health_status, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Status_and_Functioning.dta")
  490. health_care, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Care_and_Insurance.dta")
  491. exp_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/exp_income_wealth.dta")
  492. weight, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Weights.dta")
  493. houseing, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Housing_Characteristics.dta")
  494. child, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Child.dta")
  495. Exit_Interview, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Exit_Interview.dta")
  496. family_information, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Information.dta")
  497. family_transfer, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Transfer.dta")
  498. household_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Household_Income.dta")
  499. individual_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Individual_Income.dta")
  500. interviewer_observation, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Interviewer_Observation.dta")
  501. Other_HHmember, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Other_HHmember.dta")
  502. Parent, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Parent.dta")
  503. Verbal_Autopsy, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Verbal_Autopsy.dta")
  504. work_retirement_and_pension, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Work_Retirement_and_Pension.dta")
  505. #性别#年龄#婚姻状况
  506. # 1 married or partnered
  507. # 0 other marital status (separated, divorced, unmarried, or widowed)
  508. demo["marital_status"] = demo.apply(lambda x : 1 if x["be001"]==1 or x["be001"]==2 or x["be001"]==7 else 0 if x["be001"] in [3,4,5,6] else np.nan, axis=1)
  509. #教育
  510. # 0 below high school
  511. # 1 high school
  512. # 2 college or above
  513. # 纠正2011年统计错误的教育
  514. demo["education_correct"] = demo.apply(lambda x : x["bd001_w2_3"] if x["bd001_w2_1"]==2 else np.nan, axis=1)
  515. demo["education_correct"] = demo["education_correct"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  516. education_correct = demo[['ID',"education_correct"]]
  517. # 按 'ID' 列合并两个表
  518. data_2011 = pd.merge(data_2011, education_correct, on='ID', how='left')
  519. # 使用 fillna() 来更新字段
  520. data_2011['education'] = data_2011['education_correct'].fillna(data_2011['education'])
  521. # 删除多余的列
  522. data_2011 = data_2011.drop(columns=['education_correct'])
  523. #更新2013的教育
  524. demo["education"] = demo.apply(lambda x : x["bd001"] if pd.isna(x["bd001_w2_1"]) else x["bd001_w2_4"] if not pd.isna(x["bd001_w2_4"]) and not x["bd001_w2_4"]==12 else np.nan, axis=1)
  525. demo["education"] = demo["education"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  526. #合并2011年的教育
  527. eductaion_2011 = data_2011[['ID',"education"]]
  528. # 按 'ID' 列合并两个表
  529. demo = pd.merge(demo, eductaion_2011, on='ID', how='left', suffixes=("_2013","_2011"))
  530. # 使用 fillna() 来更新字段
  531. demo['education'] = demo['education_2013'].fillna(demo['education_2011'])
  532. # 纠正2011年统计错误的出生年
  533. demo["birth_year"] = demo.apply(lambda x : x["ba002_1"] if not pd.isna(x["ba002_1"]) else np.nan, axis=1)
  534. demo["birth_month"] = demo.apply(lambda x : x["ba002_2"] if not pd.isna(x["ba002_2"]) else np.nan, axis=1)
  535. birth_year_2013 = demo[['ID',"birth_year", "birth_month"]]
  536. # # 按 'ID' 列合并两个表
  537. data_2011 = pd.merge(data_2011, birth_year_2013, on='ID', how='left', suffixes=("_2011","_2013"))
  538. # # 使用 fillna() 来更新字段
  539. data_2011['birth_year'] = data_2011['birth_year_2013'].fillna(data_2011['birth_year_2011'])
  540. data_2011['birth_month'] = data_2011['birth_month_2013'].fillna(data_2011['birth_month_2011'])
  541. # # 删除多余的列
  542. data_2011 = data_2011.drop(columns=['birth_year_2013', 'birth_year_2011', 'birth_month_2013', 'birth_month_2011'])
  543. #合并2011年的出生年
  544. birth_year_2011 = data_2011[['ID',"birth_year", "birth_month"]]
  545. # 按 'ID' 列合并两个表
  546. demo = pd.merge(demo, birth_year_2011, on='ID', how='left', suffixes=("_2013","_2011"))
  547. # 使用 fillna() 来更新字段
  548. demo['birth_year'] = demo['birth_year_2013'].fillna(demo['birth_year_2011'])
  549. demo['birth_month'] = demo['birth_month_2013'].fillna(demo['birth_month_2011'])
  550. #获取随访时间
  551. demo = pd.merge(demo, weight[["ID", "iyear", "imonth"]], on = "ID", how="left")
  552. data_2013 = demo[['ID','householdID', 'communityID','ba000_w2_3','birth_year','birth_month','ba003',"iyear", "imonth", 'marital_status', "education"]]
  553. #居住地
  554. # 0 农村
  555. # 1 城市
  556. data_2013 = pd.merge(data_2013, psu[['communityID', 'province', 'city', 'urban_nbs']], on = "communityID", how="left")
  557. #身高#体重#收缩压#舒张压
  558. biomarkers["qi002"] = biomarkers["qi002"].apply(lambda x : np.nan if x >210 else x)
  559. biomarkers["ql002"] = biomarkers["ql002"].apply(lambda x : np.nan if x >150 else x)
  560. #腰围
  561. biomarkers['waist'] = biomarkers["qm002"].apply(lambda x : np.nan if x >210 else x)
  562. #血压测量后两次的平均
  563. biomarkers["qa007"] = biomarkers["qa007"].apply(lambda x : np.nan if x >300 else x)
  564. biomarkers["qa011"] = biomarkers["qa011"].apply(lambda x : np.nan if x >300 else x)
  565. biomarkers["qa008"] = biomarkers["qa008"].apply(lambda x : np.nan if x >150 else x)
  566. biomarkers["qa012"] = biomarkers["qa012"].apply(lambda x : np.nan if x >150 else x)
  567. biomarkers["Systolic"] = (biomarkers["qa007"] + biomarkers["qa011"]) /2
  568. biomarkers["Diastolic"] = (biomarkers["qa008"] + biomarkers["qa012"]) /2
  569. #受试者可以在不用手臂支撑的情况下按其平时的节奏连续起立坐下五次吗
  570. # yes
  571. biomarkers["Sit_Stand_5x_yes"] = biomarkers["qh003"]
  572. # no
  573. biomarkers["Sit_Stand_5x_no"] = biomarkers["qh004"]
  574. # times
  575. biomarkers["Sit_Stand_5x_times"] = biomarkers["qh005"]
  576. # 步行速度时间
  577. biomarkers["Walking_Speed_Time_1"] = biomarkers["qg002"]
  578. biomarkers["Walking_Speed_Time_2"] = biomarkers["qg003"]
  579. biomarkers_select = biomarkers[['ID','householdID', 'communityID','qi002','ql002', 'waist','Systolic','Diastolic', "Sit_Stand_5x_yes", "Sit_Stand_5x_no",
  580. "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2"]]
  581. data_2013 = pd.merge(data_2013, biomarkers_select, on = ["ID", "householdID", "communityID"], how="left")
  582. #白细胞(WBC),平均红血球容积MCV,血小板,血尿素氮bun,葡萄糖glu,血肌酐crea,总胆固醇cho,甘油三酯tg,高密度脂蛋白HDL,低密度脂蛋白胆固醇LDL,C反应蛋白CRP
  583. #糖化血红蛋白hba1c,尿酸ua,血细胞比容Hematocrit,血红蛋白hgb,胱抑素C
  584. data_2013[['bl_wbc','bl_mcv','bl_plt','bl_bun','bl_glu','bl_crea','bl_cho', 'bl_tg', 'bl_hdl', 'bl_ldl','bl_crp','bl_hbalc','bl_ua', 'bl_hct', 'bl_hgb','bl_cysc']]=np.nan
  585. # 慢性病:
  586. # (1) Hypertension 高血压病
  587. # (2) Dyslipidemia (elevation of low density lipoprotein, triglycerides (TGs),and total cholesterol, or a low high density lipoprotein level)血脂异常(包括低密度脂蛋白、甘油三酯、总胆固醇的升高或(和)高密度脂蛋白的下降)
  588. # (3) Diabetes or high blood sugar糖尿病或血糖升高(包括糖耐量异常和空腹血糖升高)
  589. # (4) Cancer or malignant tumor (excluding minor skin cancers) 癌症等恶性肿瘤(不包括轻度皮肤癌)
  590. # (5) Chronic lung diseases, such as chronic bronchitis , emphysema ( excluding tumors, or cancer) 慢性肺部疾患如慢性支气管炎或肺气肿、肺心病(不包括肿瘤或癌)
  591. # (6) Liver disease (except fatty liver, tumors, and cancer) 肝脏疾病
  592. # (除脂肪肝、肿瘤或癌外)
  593. # (7) Heart attack, coronary heart disease, angina, congestive heart failure, or other heart problems 心脏病(如心肌梗塞、冠心病、心绞痛、充血性心力衰竭和其他心脏疾病)
  594. # (8) Stroke 中风
  595. # (9) Kidney disease (except for tumor or cancer) 肾脏疾病(不包括肿瘤或癌)
  596. # (10) Stomach or other digestive disease (except for tumor or cancer) 胃部疾病或消化系统疾病(不包括肿瘤或癌)
  597. # (11) Emotional, nervous, or psychiatric problems 情感及精神方面问题
  598. # (12) Memory-related disease 与记忆相关的疾病 (如老年痴呆症、脑萎缩、帕金森症)
  599. # (13) Arthritis or rheumatism 关节炎或风湿病
  600. # (14) Asthma 哮喘
  601. # 体力活动
  602. # 2 vigorous (vigorous activity more than once a week)
  603. # 1 moderate (moderate activity more than once a week)
  604. # 0 inactive (the rest)
  605. health_status["Physical_activity"] = health_status.apply(lambda x : 2 if x["da051_1_"]==1 else
  606. 1 if x["da051_2_"]==1 else
  607. 0 if x["da051_3_"] == 1 or (x["da051_1_"]==2 and x["da051_2_"]==2 and x["da051_3_"] == 2)
  608. else np.nan ,axis=1)
  609. # 抽烟
  610. # 1 抽过烟
  611. # 0 没有抽过烟
  612. health_status["Smoke"] = health_status["da059"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else 1)
  613. # 喝酒
  614. # 1 喝过酒
  615. # 0 没有喝过酒
  616. health_status["Drink"] = health_status.apply(lambda x : 1 if x["da067"] ==1 or x["da067"] ==2 else
  617. 0 if x["da069"] == 1 else
  618. 1 if x["da069"] == 2 or x["da069"] == 3 else np.nan, axis=1)
  619. # 您是否经历过交通事故,或任何的重大意外伤害,并接受了治疗?
  620. # 1 是
  621. # 0 否
  622. health_status['Accident_Or_Injury']=health_status["da021"].apply(lambda x : 1 if x ==1 else
  623. 0 if x == 2 else np.nan)
  624. # 过去两年有没有摔倒?
  625. # 1 是
  626. # 0 否
  627. health_status['Fell_In_Last2Years']=health_status["da023"].apply(lambda x : 1 if x ==1 else
  628. 0 if x == 2 else np.nan)
  629. # 您什么时候开始来月经的?(year/age)
  630. health_status['Menarche_Year']=health_status["da026_1"]
  631. health_status['Menarche_Age']=health_status["da026_2"]
  632. # 您什么时候开始绝经的?
  633. health_status['Menopause_Year']=health_status["da028_1"]
  634. health_status['Menopause_Age']=health_status["da028_2"]
  635. # 第一次诊断出您有前列腺疾病是在什么时候?
  636. health_status['Prostate_Issue_Year']=health_status["da030_1"]
  637. health_status['Prostate_Issue_Age']=health_status["da030_2"]
  638. # 是否戴眼镜(包括矫正视力镜片)?
  639. # 1 是
  640. # 0 否
  641. # 2 失明
  642. # 3 偶尔
  643. health_status['Wear_Glasses']=health_status["da032"].apply(lambda x : 1 if x == 1 else 2 if x ==2 else 0 if x == 3 else 3 if x == 4 else np.nan)
  644. # 过去一个月内,您平均每天晚上真正睡着的时间大约是几小时?(可能短于您在床上躺着的时间)
  645. health_status['Average_Sleep_Hours']=health_status["da049"]
  646. # 过去一个月内,您通常午睡多长时间?分钟
  647. health_status['Average_Nap_Minutes']=health_status["da050"]
  648. # 您通常每周有没有至少持续做激烈活动十分钟?
  649. health_status['Vigorous_Activity_10Min']=health_status["da051_1_"]
  650. # 您通常每周有没有至少持续做中等强度的体力活动十分钟?
  651. health_status['Moderate_Effort_10Min']=health_status["da051_2_"]
  652. # 您通常每周有没有至少持续走路十分钟?
  653. health_status['Walking_10Min']=health_status["da051_3_"]
  654. # 您通常每周有多少天做[激烈活动]至少十分钟?
  655. health_status['Vigorous_Activity_Days']=health_status["da052_1_"]
  656. # 您通常每周有多少天做[中等强度的体力活动]至少十分钟?
  657. health_status['Moderate_Effort_Days']=health_status["da052_2_"]
  658. # 您通常每周有多少天做[走路]至少十分钟?
  659. health_status['Walking_Days']=health_status["da052_3_"]
  660. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 2小时
  661. health_status['Vigorous_Activity_2Hours_PerDay']=health_status["da053_1_"]
  662. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 2小时
  663. health_status['Moderate_Effort_2Hours_PerDay']=health_status["da053_2_"]
  664. # 在做[走路]的这些天里,您一天花多少时间做[走路] 2小时
  665. health_status['Walking_2Hours_PerDay']=health_status["da053_3_"]
  666. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 30分钟
  667. health_status['Vigorous_Activity_30Min_PerDay']=health_status["da054_1_"]
  668. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 30分钟
  669. health_status['Moderate_Effort_30Min_PerDay']=health_status["da054_2_"]
  670. # 在做[走路]的这些天里,您一天花多少时间做[走路] 30分钟
  671. health_status['Walking_30Min_PerDay']=health_status["da054_3_"]
  672. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 4小时
  673. health_status['Vigorous_Activity_4Hours_PerDay']=health_status["da055_1_"]
  674. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 4小时
  675. health_status['Moderate_Effort_4Hours_PerDay']=health_status["da055_2_"]
  676. # 在做[走路]的这些天里,您一天花多少时间做[走路] 4小时
  677. health_status['Walking_4Hours_PerDay']=health_status["da055_3_"]
  678. # 活动的原因
  679. # 1 工作需要
  680. # 2 娱乐
  681. # 3 体育锻炼
  682. # 4 其他
  683. health_status["Reason_For_Vigorous_Activity"]= health_status["da051_1_1_"]
  684. health_status["Reason_For_Moderate_Effort"]= health_status["da051_1_2_"]
  685. health_status["Reason_For_Walking"]= health_status["da051_1_3_"]
  686. # 过去一个月是否进行了下列社交活动?
  687. # (1) 串门、跟朋友交往
  688. # (2) 打麻将、下棋、打牌、去社区活动室
  689. # (3) 无偿向与您不住在一起的亲人、朋友或者邻居提供帮助
  690. # (4) 去公园或者其他场所跳舞、健身、练气功等
  691. # (5) 参加社团组织活动
  692. # (6) 志愿者活动或者慈善活动/无偿照顾与您不住在一起的病人或残疾人
  693. # (7) 上学或者参加培训课程
  694. # (8)其他
  695. # (9) 以上均没有
  696. health_status["da056s1"] = health_status.apply(lambda x: 1 if x["da056s1"]==1 else 0, axis=1)
  697. health_status["da056s2"] = health_status.apply(lambda x: 2 if x["da056s2"]==2 else 0, axis=1)
  698. health_status["da056s3"] = health_status.apply(lambda x: 3 if x["da056s3"]==3 else 0, axis=1)
  699. health_status["da056s4"] = health_status.apply(lambda x: 4 if x["da056s4"]==4 else 0, axis=1)
  700. health_status["da056s5"] = health_status.apply(lambda x: 5 if x["da056s5"]==5 else 0, axis=1)
  701. health_status["da056s6"] = health_status.apply(lambda x: 6 if x["da056s6"]==6 or x["da056s7"]==7 else 0, axis=1)
  702. health_status["da056s7"] = health_status.apply(lambda x: 7 if x["da056s8"]==8 else 0, axis=1)
  703. health_status["da056s8"] = health_status.apply(lambda x: 8 if x["da056s9"]==9 or x["da056s10"]==10 or x["da056s11"]==11 else 0, axis=1)
  704. health_status["da056s9"] = health_status.apply(lambda x: 7 if x["da056s12"]==12 else 0, axis=1)
  705. # 过去一个月的活动频率
  706. # (1) Almost daily 差不多每天
  707. # (2) Almost every week 差不多每周
  708. # (3) Not regularly 不经常
  709. health_status["da057_6_"] = health_status.apply(lambda x: 1 if x["da057_6_"]==1 or x["da057_7_"]==1 else 2 if x["da057_6_"]==2 or x["da057_7_"]==2 else 3 if x["da057_6_"]==3 or x["da057_7_"]==3 else np.nan, axis=1)
  710. health_status["da057_7_"] = health_status["da057_8_"]
  711. health_status["da057_8_"] = health_status.apply(lambda x: 1 if x["da057_9_"]==1 or x["da057_10_"]==1 or x["da057_11_"]==1
  712. else 2 if x["da057_9_"]==2 or x["da057_10_"]==2 or x["da057_11_"]==2
  713. else 3 if x["da057_9_"]==3 or x["da057_10_"]==3 or x["da057_11_"]==3
  714. else np.nan, axis=1)
  715. # 过去一个月,您是否上网?
  716. health_status["Internet_Usage_LastMonth"] = health_status["da056s10"].apply(lambda x : 1 if x==10 else 0)
  717. # 使用以下哪些工具上网?
  718. health_status[["Internet_Tools_Desktop_computer"]] = np.nan
  719. health_status[["Internet_Tools_Laptop_computer"]] = np.nan
  720. health_status[["Internet_Tools_Tablet_computer"]] = np.nan
  721. health_status[["Internet_Tools_Cellphone"]] = np.nan
  722. health_status[["Internet_Tools_Other"]] = np.nan
  723. # 上网一般做什么?
  724. health_status[["Internet_Purpose_Chat"]] = np.nan
  725. health_status[["Internet_Purpose_news"]] = np.nan
  726. health_status[["Internet_Purpose_videos"]] = np.nan
  727. health_status[["Internet_Purpose_games"]] = np.nan
  728. health_status[["Internet_Purpose_Financial"]] = np.nan
  729. health_status[["Internet_Purpose_Others"]] = np.nan
  730. # 是否会用手机支付
  731. health_status[["Mobile_Payment"]] = np.nan
  732. # 是否使用微信?
  733. health_status[["Wechat_Usage"]] = np.nan
  734. # 发不发微信朋友圈?
  735. health_status[["Post_Moments"]] = np.nan
  736. # 现在还在吸烟还是戒烟了?
  737. # 1 仍然抽烟 Skip DA062 请跳过DA062
  738. # 2 戒烟
  739. health_status['Current_Smoking_Status']=health_status["da061"]
  740. # 吸烟时,一般抽什么烟?
  741. # (1) Smoking a pipe 用烟管吸烟(烟袋、旱烟)
  742. # (2) Smoking self-rolled cigarettes 自己卷烟抽
  743. # (3) Filtered cigarette带滤咀香烟
  744. # (4) Unfiltered cigarette不带滤咀香烟
  745. # (5) Cigar雪茄
  746. # (6) Water cigarettes 水烟
  747. health_status[['Smoking_Type_pipe']]=np.nan
  748. health_status[['Smoking_Type_rolled']]=np.nan
  749. health_status[['Smoking_Type_Filtered']]=np.nan
  750. health_status[['Smoking_Type_Unfiltered']]=np.nan
  751. health_status[['Smoking_Type_Cigar']]=np.nan
  752. health_status[['Smoking_Type_Water']]=np.nan
  753. # 现在/戒烟前平均一天抽多少支香烟?
  754. health_status['Daily_Cigarette_Count']=health_status["da063"]
  755. # 在过去的一年, 喝酒吗
  756. # (1) Drink more than once a month. 喝酒,每月超过一次
  757. # (2) Drink but less than once a month 喝酒,但每月少于一次
  758. # (3) None of these 什么都不喝
  759. health_status['Drink_PastYear']=health_status["da067"]
  760. # 过去一年内 平均一个月喝几次酒
  761. # (1)Once a month 每月一次
  762. # (2)2-3 times a month 每月2-3次
  763. # (3)Once a week 每周一次
  764. # (4)2-3 times a week 每周2-3次
  765. # (5)4-6 times a week 每周4-6次
  766. # (6)Once a day 每天一次
  767. # (7)Twice a day 一天两次
  768. # (8)More than twice a day 一天超过两次
  769. health_status['Drink_Monthly_Frequency']=health_status.apply(lambda x : 8 if x["da072"] ==8 or x["da074"] ==8 or x["da076"] ==8 else
  770. 7 if x["da072"] ==7 or x["da074"] ==7 or x["da076"] ==7 else
  771. 6 if x["da072"] ==6 or x["da074"] ==6 or x["da076"] ==6 else
  772. 5 if x["da072"] ==5 or x["da074"] ==5 or x["da076"] ==5 else
  773. 4 if x["da072"] ==4 or x["da074"] ==4 or x["da076"] ==4 else
  774. 3 if x["da072"] ==3 or x["da074"] ==3 or x["da076"] ==3 else
  775. 2 if x["da072"] ==2 or x["da074"] ==2 or x["da076"] ==2 else
  776. 1 if x["da072"] ==1 or x["da074"] ==1 or x["da076"] ==1 else np.nan, axis=1)
  777. # 自上次访问以来的两年内,您是否发作过心脏病?
  778. # 1 是
  779. # 0 否
  780. health_status["Heart_attack_2_years"] = health_status.apply(lambda x : 1 if x["da007_w2_5"] ==1 else
  781. 0 if x["da007_w2_5"] == 2 else np.nan, axis=1)
  782. # 自上次访问以来,是否有医生诊断您中风复发?
  783. # 1 是
  784. # 0 否
  785. health_status['Recurrent_Stroke']=health_status.apply(lambda x : 1 if x["da019_w2_1"] ==1 else
  786. 0 if x["da019_w2_1"] == 2 else np.nan, axis=1)
  787. # 合并2011年的慢性病
  788. columns_to_diseases_old = ['da007_1_', 'da007_2_','da007_3_','da007_4_','da007_5_','da007_6_','da007_7_','da007_8_','da007_9_','da007_10_','da007_11_'
  789. ,'da007_12_','da007_13_','da007_14_']
  790. columns_to_diseases_new = ['Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  791. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  792. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']
  793. for (col_old, col_new) in zip(columns_to_diseases_old,columns_to_diseases_new):
  794. health_status[col_new] = health_status.apply(lambda x : x[col_old] if not pd.isna(x[col_old]) else np.nan, axis=1)
  795. diseases_2011 = data_2011[['ID','Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  796. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  797. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']]
  798. # 按 'ID' 列合并两个表
  799. health_status = pd.merge(health_status, diseases_2011, on='ID', how='left', suffixes=("_2013","_2011"))
  800. # 使用 fillna() 来更新字段
  801. for col in columns_to_diseases_new:
  802. health_status[col] = health_status[f'{col}_2013'].fillna(health_status[f'{col}_2011'])
  803. health_status_select = health_status[['ID','householdID', 'communityID', 'Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  804. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  805. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma', "Physical_activity", "Smoke", "Drink", "Accident_Or_Injury", "Fell_In_Last2Years", "Menarche_Year", "Menarche_Age",
  806. "Menopause_Year", "Menopause_Age", "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses" ,
  807. "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  808. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  809. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  810. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  811. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  812. , "da056s1", "da056s2", "da056s3", "da056s4", "da056s5", "da056s6", "da056s7", "da056s8", "da056s9"
  813. , "da057_1_" , "da057_2_", "da057_3_", "da057_4_", "da057_5_", "da057_6_", "da057_7_", "da057_8_"
  814. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  815. , "Internet_Purpose_Chat", "Internet_Purpose_news","Internet_Purpose_videos","Internet_Purpose_games","Internet_Purpose_Financial","Internet_Purpose_Others"
  816. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  817. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  818. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency", "Heart_attack_2_years", "Recurrent_Stroke"]]
  819. data_2013 = pd.merge(data_2013, health_status_select, on = ["ID", 'householdID', 'communityID'], how="left")
  820. #计算认知功能得分,分成三部分:电话问卷9分,词语回忆10分、画图1分
  821. health_status["dc001s1_score"] = health_status["dc001s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  822. health_status["dc001s2_score"] = health_status["dc001s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  823. health_status["dc001s3_score"] = health_status["dc001s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  824. health_status["dc002_score"] = health_status["dc002"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  825. # health_status["dc003_score"] = health_status["dc003"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  826. health_status["dc019_score"] = health_status["dc019"].apply(lambda x : 1 if x==93 else 0 if pd.isna(x) else 0)
  827. health_status["dc020_score"] = health_status["dc020"].apply(lambda x : 1 if x==86 else 0 if pd.isna(x) else 0)
  828. health_status["dc021_score"] = health_status["dc021"].apply(lambda x : 1 if x==79 else 0 if pd.isna(x) else 0)
  829. health_status["dc022_score"] = health_status["dc022"].apply(lambda x : 1 if x==72 else 0 if pd.isna(x) else 0)
  830. health_status["dc023_score"] = health_status["dc023"].apply(lambda x : 1 if x==65 else 0 if pd.isna(x) else 0)
  831. #词语记忆
  832. health_status["dc006s1_score"] = health_status["dc006_1_s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  833. health_status["dc006s2_score"] = health_status["dc006_1_s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  834. health_status["dc006s3_score"] = health_status["dc006_1_s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  835. health_status["dc006s4_score"] = health_status["dc006_1_s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  836. health_status["dc006s5_score"] = health_status["dc006_1_s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  837. health_status["dc006s6_score"] = health_status["dc006_1_s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  838. health_status["dc006s7_score"] = health_status["dc006_1_s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  839. health_status["dc006s8_score"] = health_status["dc006_1_s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  840. health_status["dc006s9_score"] = health_status["dc006_1_s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  841. health_status["dc006s10_score"] = health_status["dc006_1_s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  842. # health_status["dc006s11_score"] = health_status["dc006_1_s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  843. health_status["dc027s1_score"] = health_status["dc027s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  844. health_status["dc027s2_score"] = health_status["dc027s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  845. health_status["dc027s3_score"] = health_status["dc027s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  846. health_status["dc027s4_score"] = health_status["dc027s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  847. health_status["dc027s5_score"] = health_status["dc027s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  848. health_status["dc027s6_score"] = health_status["dc027s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  849. health_status["dc027s7_score"] = health_status["dc027s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  850. health_status["dc027s8_score"] = health_status["dc027s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  851. health_status["dc027s9_score"] = health_status["dc027s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  852. health_status["dc027s10_score"] = health_status["dc027s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  853. # health_status["dc027s11_score"] = health_status["dc027s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  854. #画图
  855. health_status["draw_score"] = health_status["dc025"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  856. data_2013["Cognition_score"] = health_status["dc001s1_score"] + health_status["dc001s2_score"] + \
  857. health_status["dc001s3_score"] + health_status["dc002_score"]+ \
  858. health_status["dc019_score"]+ health_status["dc020_score"] + health_status["dc021_score"]+ \
  859. health_status["dc022_score"]+ health_status["dc023_score"] + health_status["dc006s1_score"] + \
  860. health_status["dc006s2_score"] + health_status["dc006s3_score"] + health_status["dc006s4_score"] + \
  861. health_status["dc006s5_score"] + health_status["dc006s6_score"] + health_status["dc006s7_score"] + \
  862. health_status["dc006s8_score"] + health_status["dc006s9_score"] + health_status["dc006s10_score"] + \
  863. health_status["dc027s1_score"]+ health_status["dc027s2_score"]+ \
  864. health_status["dc027s3_score"]+ health_status["dc027s4_score"]+ health_status["dc027s5_score"]+ \
  865. health_status["dc027s6_score"]+ health_status["dc027s7_score"]+ health_status["dc027s8_score"]+ \
  866. health_status["dc027s9_score"]+health_status["dc027s10_score"]+\
  867. health_status["draw_score"]
  868. #心理得分
  869. health_status["dc009_score"] = health_status["dc009"]-1
  870. health_status["dc010_score"] = health_status["dc010"]-1
  871. health_status["dc011_score"] = health_status["dc011"]-1
  872. health_status["dc012_score"] = health_status["dc012"]-1
  873. health_status["dc013_score"] = 4 - health_status["dc013"]
  874. health_status["dc014_score"] = health_status["dc014"]-1
  875. health_status["dc015_score"] = health_status["dc015"]-1
  876. health_status["dc016_score"] = 4 - health_status["dc016"]
  877. health_status["dc017_score"] = health_status["dc017"]-1
  878. health_status["dc018_score"] = health_status["dc018"]-1
  879. data_2013["psychiatric_score"] = health_status["dc009_score"] + health_status["dc010_score"] + health_status["dc011_score"] + \
  880. health_status["dc012_score"] + health_status["dc013_score"] + health_status["dc014_score"] + health_status["dc015_score"] + \
  881. health_status["dc016_score"] + health_status["dc017_score"] + health_status["dc018_score"]
  882. #睡眠状态
  883. # (1)Rarely or none of the time (<1 day) 很少或者根本没有(<1天)
  884. # (2)Some or a little of the time (1-2 days) 不太多(1-2天)
  885. # (3)Occasionally or a moderate amount of the time (3-4 days) 有时或者说有一半的时间(3-4天)
  886. # (4)Most or all of the time (5-7 days) 大多数的时间(5-7天)
  887. data_2013["sleep_state"] = health_status['dc015']
  888. #ADL
  889. health_status["db010_score"] = health_status["db010"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  890. health_status["db011_score"] = health_status["db011"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  891. health_status["db012_score"] = health_status["db012"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  892. health_status["db013_score"] = health_status["db013"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  893. health_status["db014_score"] = health_status["db014"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  894. health_status["db015_score"] = health_status["db015"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  895. data_2013["ADL"] = health_status["db010_score"] + health_status["db011_score"] + health_status["db012_score"] + health_status["db013_score"] + \
  896. health_status["db014_score"] + health_status["db015_score"]
  897. # 是否有管道煤气或天然气?
  898. houseing["Gas_Connection"] = houseing["i019"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  899. # 是否带供暖设施(不包括土暖气和可制暖的空调)?
  900. houseing["Heating_Facility"] = houseing["i020"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  901. # 供暖所用的主要能源是什么?
  902. # (1)Solar 太阳能
  903. # (2)Coal 煤炭、蜂窝煤
  904. # (3)Natural gas 管道天然气或煤气
  905. # (4)Liquefied Petroleum Gas 液化石油气
  906. # (5)Electric 电
  907. # (6)Crop residue/Wood buring 秸秆、柴火
  908. # (7)Other 其他
  909. houseing["Heating_Energy"] = houseing["i021"]
  910. # 做饭用的主要燃料是什么?
  911. # (1)Coal 煤炭、蜂窝煤
  912. # (2)Natural gas 管道天然气或煤气
  913. # (3)Marsh gas 沼气
  914. # (4)Liquefied Petroleum Gas 液化石油气
  915. # (5)Electric 电
  916. # (6)crop residue/Wood burning 秸秆、柴火
  917. # (7)other 其他
  918. houseing["Cooking_Fuel"] = houseing["i022"]
  919. houseing_select = houseing[['ID','householdID', 'communityID','Gas_Connection',
  920. 'Heating_Facility', 'Heating_Energy', 'Cooking_Fuel']]
  921. data_2013 = pd.merge(data_2013, houseing_select, on = ["ID", 'householdID', 'communityID'], how="left")
  922. data_2013["wave"] = year
  923. change_columns(data_2013)
  924. # 将全部数据进行合并
  925. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Demographic_Background.dta")
  926. data_2013 = pd.merge(data_2013, demo, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_demo'))
  927. data_2013 = pd.merge(data_2013, psu[["communityID", "areatype"]], on = ['communityID'], how="left", suffixes=('', '_psu'))
  928. data_2013 = pd.merge(data_2013, biomarkers, on = ["ID", "householdID", "communityID"], how="left", suffixes=('', '_bio'))
  929. data_2013 = pd.merge(data_2013, health_status, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_statush'))
  930. data_2013 = pd.merge(data_2013, health_care, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_careh'))
  931. data_2013 = pd.merge(data_2013, exp_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomee'))
  932. data_2013 = pd.merge(data_2013, weight, on = ["ID", 'householdID', 'communityID', "iyear", "imonth"], how="left", suffixes=('', '_weightw'))
  933. data_2013 = pd.merge(data_2013, houseing, on = ['householdID', 'communityID'], how="left", suffixes=('', '_houseing'))
  934. # data_2013 = pd.merge(data_2013, child, on = ['communityID'], how="left", suffixes=('', '_child'))
  935. data_2013 = pd.merge(data_2013, Exit_Interview, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_exit'))
  936. data_2013 = pd.merge(data_2013, family_information, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_information'))
  937. data_2013 = pd.merge(data_2013, family_transfer, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_transfer'))
  938. data_2013 = pd.merge(data_2013, household_income, on = ['householdID', 'communityID'], how="left", suffixes=('', '_incomeh'))
  939. data_2013 = pd.merge(data_2013, individual_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomei'))
  940. data_2013 = pd.merge(data_2013, interviewer_observation, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_observation'))
  941. data_2013 = pd.merge(data_2013, work_retirement_and_pension, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_pension'))
  942. # data_2013 = pd.merge(data_2013, Other_HHmember, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Othermember'))
  943. # data_2013 = pd.merge(data_2013, Parent, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Parent'))
  944. data_2013 = pd.merge(data_2013, Verbal_Autopsy, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Verbal'))
  945. # data_2013 = pd.concat([data_2011, data_2013], axis=0)
  946. #修改地区名称
  947. #省份、城市名称和污染物数据格式对齐
  948. #海东地区->海东市
  949. data_2013['city'] = data_2013['city'].replace('海东地区', '海东市')
  950. #北京 -> 北京市
  951. data_2013['city'] = data_2013['city'].replace('北京', '北京市')
  952. data_2013['province'] = data_2013['province'].replace('北京', '北京市')
  953. #哈尔滨 -> 哈尔滨市
  954. data_2013['city'] = data_2013['city'].replace('哈尔滨', '哈尔滨市')
  955. #天津 -> 天津市
  956. data_2013['city'] = data_2013['city'].replace('天津', '天津市')
  957. data_2013['province'] = data_2013['province'].replace('天津', '天津市')
  958. #广西省 -> 广西壮族自治区
  959. data_2013['province'] = data_2013['province'].replace('广西省', '广西壮族自治区')
  960. #巢湖市 -> 合肥市
  961. data_2013['city'] = data_2013['city'].replace('巢湖市', '合肥市')
  962. #襄樊市->襄阳市
  963. data_2013['city'] = data_2013['city'].replace('襄樊市', '襄阳市')
  964. # data_2011.to_csv("2011_all_charls.csv", index=False)
  965. # data_2013.to_csv("2013_all_charls.csv", index=False)
  966. print("2013 complete")
  967. # 2015年
  968. year = "2015"
  969. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Demographic_Background.dta")
  970. psu, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS2013/PSU.dta", encoding='gbk')
  971. blood, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Blood.dta")
  972. child, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Child.dta")
  973. family_information, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Information.dta")
  974. family_transfer, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Transfer.dta")
  975. household_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Household_Income.dta")
  976. Household_Member, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Household_Member.dta")
  977. biomarkers, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Biomarker.dta")
  978. health_status, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Status_and_Functioning.dta")
  979. health_care, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Care_and_Insurance.dta")
  980. weight, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Weights.dta")
  981. houseing, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Housing_Characteristics.dta")
  982. individual_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Individual_Income.dta")
  983. Parent, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Parent.dta")
  984. Sample_Infor, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Sample_Infor.dta")
  985. Sibling, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Sibling.dta")
  986. Spousal_Sibling, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Spousal_Sibling.dta")
  987. work_retirement_and_pension, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Work_Retirement_and_Pension.dta")
  988. #性别#年龄#婚姻状况
  989. # 1 married or partnered
  990. # 0 other marital status (separated, divorced, unmarried, or widowed)
  991. demo["marital_status"] = demo.apply(lambda x : 1 if x["be001"]==1 or x["be001"]==2 or x["be001"]==7 else 0 if x["be001"] in [3,4,5,6] else np.nan, axis=1)
  992. #教育
  993. # 0 below high school
  994. # 1 high school
  995. # 2 college or above
  996. #更新2015的教育
  997. demo["education"] = demo.apply(lambda x : x["bd001_w2_4"] if not pd.isna(x["bd001_w2_4"]) and not x["bd001_w2_4"]==12 else np.nan, axis=1)
  998. demo["education"] = demo["education"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  999. #合并2013年的教育
  1000. eductaion_2013 = data_2013[data_2013["wave"]=="2013"][['ID',"education"]]
  1001. # 按 'ID' 列合并两个表
  1002. demo = pd.merge(demo, eductaion_2013, on='ID', how='left', suffixes=("_2015","_2013"))
  1003. # 使用 fillna() 来更新字段
  1004. demo['education'] = demo['education_2015'].fillna(demo['education_2013'])
  1005. # 2015年的出生年
  1006. demo["birth_year"] = demo.apply(lambda x : x["ba004_w3_1"] if x["ba002"]==1 else x["ba002_1"] if x["ba002"]==2 else np.nan, axis=1)
  1007. demo["birth_month"] = demo.apply(lambda x : x["ba004_w3_2"] if x["ba002"]==1 else x["ba002_2"] if x["ba002"]==2 else np.nan, axis=1)
  1008. #获取随访时间
  1009. demo = pd.merge(demo, Sample_Infor[["ID", "iyear", "imonth"]], on = "ID", how="left")
  1010. data_2015 = demo[['ID','householdID', 'communityID','ba000_w2_3', 'birth_year','birth_month','ba003',"iyear", "imonth", 'marital_status', 'education']]
  1011. #居住地
  1012. # 0 农村
  1013. # 1 城市
  1014. data_2015 = pd.merge(data_2015, psu[['communityID', 'province', 'city', 'urban_nbs']], on = "communityID", how="left")
  1015. #身高#体重#收缩压#舒张压
  1016. biomarkers["qi002"] = biomarkers["qi002"].apply(lambda x : np.nan if x >210 else x)
  1017. biomarkers["ql002"] = biomarkers["ql002"].apply(lambda x : np.nan if x >150 else x)
  1018. #腰围
  1019. biomarkers['waist'] = biomarkers["qm002"].apply(lambda x : np.nan if x >210 else x)
  1020. #血压测量后两次的平均
  1021. biomarkers["qa007"] = biomarkers["qa007"].apply(lambda x : np.nan if x >300 else x)
  1022. biomarkers["qa011"] = biomarkers["qa011"].apply(lambda x : np.nan if x >300 else x)
  1023. biomarkers["qa008"] = biomarkers["qa008"].apply(lambda x : np.nan if x >150 else x)
  1024. biomarkers["qa012"] = biomarkers["qa012"].apply(lambda x : np.nan if x >150 else x)
  1025. biomarkers["Systolic"] = (biomarkers["qa007"] + biomarkers["qa011"]) /2
  1026. biomarkers["Diastolic"] = (biomarkers["qa008"] + biomarkers["qa012"]) /2
  1027. #受试者可以在不用手臂支撑的情况下按其平时的节奏连续起立坐下五次吗
  1028. # yes
  1029. biomarkers["Sit_Stand_5x_yes"] = biomarkers["qh003"]
  1030. # no
  1031. biomarkers["Sit_Stand_5x_no"] = biomarkers["qh004"]
  1032. # times
  1033. biomarkers["Sit_Stand_5x_times"] = biomarkers["qh005"]
  1034. # 步行速度时间
  1035. biomarkers["Walking_Speed_Time_1"] = biomarkers["qg002"]
  1036. biomarkers["Walking_Speed_Time_2"] = biomarkers["qg003"]
  1037. #身高#体重#收缩压#舒张压
  1038. biomarkers_select = biomarkers[['ID','householdID', 'communityID','qi002', 'ql002', 'waist', 'Systolic','Diastolic', "Sit_Stand_5x_yes", "Sit_Stand_5x_no",
  1039. "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2"]]
  1040. data_2015 = pd.merge(data_2015, biomarkers_select, on = ["ID", "householdID", "communityID"], how="left")
  1041. #白细胞(WBC),平均红血球容积MCV,血小板,血尿素氮bun,葡萄糖glu,血肌酐crea,总胆固醇cho,甘油三酯tg,高密度脂蛋白HDL,低密度脂蛋白胆固醇LDL,C反应蛋白CRP
  1042. #糖化血红蛋白hba1c,尿酸ua,血细胞比容Hematocrit,血红蛋白hgb,胱抑素C
  1043. blood = blood[['ID', 'bl_wbc','bl_mcv','bl_plt','bl_bun','bl_glu','bl_crea','bl_cho', 'bl_tg', 'bl_hdl', 'bl_ldl','bl_crp','bl_hbalc','bl_ua', 'bl_hct', 'bl_hgb','bl_cysc']]
  1044. data_2015 = pd.merge(data_2015, blood, on = ["ID"], how="left")
  1045. # 慢性病:
  1046. # (1) Hypertension 高血压病
  1047. # (2) Dyslipidemia (elevation of low density lipoprotein, triglycerides (TGs),and total cholesterol, or a low high density lipoprotein level)血脂异常(包括低密度脂蛋白、甘油三酯、总胆固醇的升高或(和)高密度脂蛋白的下降)
  1048. # (3) Diabetes or high blood sugar糖尿病或血糖升高(包括糖耐量异常和空腹血糖升高)
  1049. # (4) Cancer or malignant tumor (excluding minor skin cancers) 癌症等恶性肿瘤(不包括轻度皮肤癌)
  1050. # (5) Chronic lung diseases, such as chronic bronchitis , emphysema ( excluding tumors, or cancer) 慢性肺部疾患如慢性支气管炎或肺气肿、肺心病(不包括肿瘤或癌)
  1051. # (6) Liver disease (except fatty liver, tumors, and cancer) 肝脏疾病
  1052. # (除脂肪肝、肿瘤或癌外)
  1053. # (7) Heart attack, coronary heart disease, angina, congestive heart failure, or other heart problems 心脏病(如心肌梗塞、冠心病、心绞痛、充血性心力衰竭和其他心脏疾病)
  1054. # (8) Stroke 中风
  1055. # (9) Kidney disease (except for tumor or cancer) 肾脏疾病(不包括肿瘤或癌)
  1056. # (10) Stomach or other digestive disease (except for tumor or cancer) 胃部疾病或消化系统疾病(不包括肿瘤或癌)
  1057. # (11) Emotional, nervous, or psychiatric problems 情感及精神方面问题
  1058. # (12) Memory-related disease 与记忆相关的疾病 (如老年痴呆症、脑萎缩、帕金森症)
  1059. # (13) Arthritis or rheumatism 关节炎或风湿病
  1060. # (14) Asthma 哮喘
  1061. # 体力活动
  1062. # 2 vigorous (vigorous activity more than once a week)
  1063. # 1 moderate (moderate activity more than once a week)
  1064. # 0 inactive (the rest)
  1065. health_status["Physical_activity"] = health_status.apply(lambda x : 2 if x["da051_1_"]==1 else
  1066. 1 if x["da051_2_"]==1 else
  1067. 0 if x["da051_3_"] == 1 or (x["da051_1_"]==2 and x["da051_2_"]==2 and x["da051_3_"] == 2)
  1068. else np.nan ,axis=1)
  1069. # 抽烟
  1070. # 1 抽过烟
  1071. # 0 没有抽过烟
  1072. health_status["Smoke"] = health_status["da059"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else 1)
  1073. # 喝酒
  1074. # 1 喝过酒
  1075. # 0 没有喝过酒
  1076. health_status["Drink"] = health_status.apply(lambda x : 1 if x["da067"] ==1 or x["da067"] ==2 else
  1077. 0 if x["da069"] == 1 else
  1078. 1 if x["da069"] == 2 or x["da069"] == 3 else np.nan, axis=1)
  1079. # 您是否经历过交通事故,或任何的重大意外伤害,并接受了治疗?
  1080. # 1 是
  1081. # 0 否
  1082. health_status['Accident_Or_Injury']=health_status["da021"].apply(lambda x : 1 if x ==1 else
  1083. 0 if x == 2 else np.nan)
  1084. # 过去两年有没有摔倒?
  1085. # 1 是
  1086. # 0 否
  1087. health_status['Fell_In_Last2Years']=health_status["da023"].apply(lambda x : 1 if x ==1 else
  1088. 0 if x == 2 else np.nan)
  1089. # 您什么时候开始来月经的?(year/age)
  1090. health_status['Menarche_Year']=health_status["da026_1"]
  1091. health_status['Menarche_Age']=health_status["da026_2"]
  1092. # 您什么时候开始绝经的?
  1093. health_status['Menopause_Year']=health_status["da028_1"]
  1094. health_status['Menopause_Age']=health_status["da028_2"]
  1095. # 第一次诊断出您有前列腺疾病是在什么时候?
  1096. health_status['Prostate_Issue_Year']=health_status["da030_1"]
  1097. health_status['Prostate_Issue_Age']=health_status["da030_2"]
  1098. # 是否戴眼镜(包括矫正视力镜片)?
  1099. # 1 是
  1100. # 0 否
  1101. # 2 失明
  1102. # 3 偶尔
  1103. health_status['Wear_Glasses']=health_status["da032"].apply(lambda x : 1 if x == 1 else 2 if x ==2 else 0 if x == 3 else 3 if x == 4 else np.nan)
  1104. # 过去一个月内,您平均每天晚上真正睡着的时间大约是几小时?(可能短于您在床上躺着的时间)
  1105. health_status['Average_Sleep_Hours']=health_status["da049"]
  1106. # 过去一个月内,您通常午睡多长时间?分钟
  1107. health_status['Average_Nap_Minutes']=health_status["da050"]
  1108. # 您通常每周有没有至少持续做激烈活动十分钟?
  1109. health_status['Vigorous_Activity_10Min']=health_status["da051_1_"]
  1110. # 您通常每周有没有至少持续做中等强度的体力活动十分钟?
  1111. health_status['Moderate_Effort_10Min']=health_status["da051_2_"]
  1112. # 您通常每周有没有至少持续走路十分钟?
  1113. health_status['Walking_10Min']=health_status["da051_3_"]
  1114. # 您通常每周有多少天做[激烈活动]至少十分钟?
  1115. health_status['Vigorous_Activity_Days']=health_status["da052_1_"]
  1116. # 您通常每周有多少天做[中等强度的体力活动]至少十分钟?
  1117. health_status['Moderate_Effort_Days']=health_status["da052_2_"]
  1118. # 您通常每周有多少天做[走路]至少十分钟?
  1119. health_status['Walking_Days']=health_status["da052_3_"]
  1120. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 2小时
  1121. health_status['Vigorous_Activity_2Hours_PerDay']=health_status["da053_1_"]
  1122. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 2小时
  1123. health_status['Moderate_Effort_2Hours_PerDay']=health_status["da053_2_"]
  1124. # 在做[走路]的这些天里,您一天花多少时间做[走路] 2小时
  1125. health_status['Walking_2Hours_PerDay']=health_status["da053_3_"]
  1126. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 30分钟
  1127. health_status['Vigorous_Activity_30Min_PerDay']=health_status["da054_1_"]
  1128. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 30分钟
  1129. health_status['Moderate_Effort_30Min_PerDay']=health_status["da054_2_"]
  1130. # 在做[走路]的这些天里,您一天花多少时间做[走路] 30分钟
  1131. health_status['Walking_30Min_PerDay']=health_status["da054_3_"]
  1132. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 4小时
  1133. health_status['Vigorous_Activity_4Hours_PerDay']=health_status["da055_1_"]
  1134. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 4小时
  1135. health_status['Moderate_Effort_4Hours_PerDay']=health_status["da055_2_"]
  1136. # 在做[走路]的这些天里,您一天花多少时间做[走路] 4小时
  1137. health_status['Walking_4Hours_PerDay']=health_status["da055_3_"]
  1138. # 活动的原因
  1139. # 1 工作需要
  1140. # 2 娱乐
  1141. # 3 体育锻炼
  1142. # 4 其他
  1143. health_status["Reason_For_Vigorous_Activity"]= health_status["da051_1_1_"]
  1144. health_status["Reason_For_Moderate_Effort"]= health_status["da051_1_2_"]
  1145. health_status["Reason_For_Walking"]= health_status["da051_1_3_"]
  1146. # 过去一个月是否进行了下列社交活动?
  1147. # (1) 串门、跟朋友交往
  1148. # (2) 打麻将、下棋、打牌、去社区活动室
  1149. # (3) 无偿向与您不住在一起的亲人、朋友或者邻居提供帮助
  1150. # (4) 去公园或者其他场所跳舞、健身、练气功等
  1151. # (5) 参加社团组织活动
  1152. # (6) 志愿者活动或者慈善活动/无偿照顾与您不住在一起的病人或残疾人
  1153. # (7) 上学或者参加培训课程
  1154. # (8)其他
  1155. # (9) 以上均没有
  1156. health_status["da056s1"] = health_status.apply(lambda x: 1 if x["da056s1"]==1 else 0, axis=1)
  1157. health_status["da056s2"] = health_status.apply(lambda x: 2 if x["da056s2"]==2 else 0, axis=1)
  1158. health_status["da056s3"] = health_status.apply(lambda x: 3 if x["da056s3"]==3 else 0, axis=1)
  1159. health_status["da056s4"] = health_status.apply(lambda x: 4 if x["da056s4"]==4 else 0, axis=1)
  1160. health_status["da056s5"] = health_status.apply(lambda x: 5 if x["da056s5"]==5 else 0, axis=1)
  1161. health_status["da056s6"] = health_status.apply(lambda x: 6 if x["da056s6"]==6 or x["da056s7"]==7 else 0, axis=1)
  1162. health_status["da056s7"] = health_status.apply(lambda x: 7 if x["da056s8"]==8 else 0, axis=1)
  1163. health_status["da056s8"] = health_status.apply(lambda x: 8 if x["da056s9"]==9 or x["da056s10"]==10 or x["da056s11"]==11 else 0, axis=1)
  1164. health_status["da056s9"] = health_status.apply(lambda x: 7 if x["da056s12"]==12 else 0, axis=1)
  1165. # 过去一个月的活动频率
  1166. # (1) Almost daily 差不多每天
  1167. # (2) Almost every week 差不多每周
  1168. # (3) Not regularly 不经常
  1169. health_status["da057_6_"] = health_status.apply(lambda x: 1 if x["da057_6_"]==1 or x["da057_7_"]==1 else 2 if x["da057_6_"]==2 or x["da057_7_"]==2 else 3 if x["da057_6_"]==3 or x["da057_7_"]==3 else np.nan, axis=1)
  1170. health_status["da057_7_"] = health_status["da057_8_"]
  1171. health_status["da057_8_"] = health_status.apply(lambda x: 1 if x["da057_9_"]==1 or x["da057_10_"]==1 or x["da057_11_"]==1
  1172. else 2 if x["da057_9_"]==2 or x["da057_10_"]==2 or x["da057_11_"]==2
  1173. else 3 if x["da057_9_"]==3 or x["da057_10_"]==3 or x["da057_11_"]==3
  1174. else np.nan, axis=1)
  1175. # 过去一个月,您是否上网?
  1176. health_status["Internet_Usage_LastMonth"] = health_status["da056s10"].apply(lambda x : 1 if x==10 else 0)
  1177. # 使用以下哪些工具上网?
  1178. # 1. Desktop computer 台式电脑
  1179. # 2. Laptop computer 笔记本电脑
  1180. # 3. Tablet computer 平板电脑(如 IPAD)
  1181. # 4. Cellphone 手机
  1182. # 5. Other devices 其他设备
  1183. health_status["Internet_Tools_Desktop_computer"] = health_status["da056_w3s1"]
  1184. health_status["Internet_Tools_Laptop_computer"] = health_status["da056_w3s2"]
  1185. health_status["Internet_Tools_Tablet_computer"] = health_status["da056_w3s3"]
  1186. health_status["Internet_Tools_Cellphone"] = health_status["da056_w3s4"]
  1187. health_status["Internet_Tools_Other"] = health_status["da056_w3s5"]
  1188. # 上网一般做什么?
  1189. health_status[["Internet_Purpose_Chat"]] = np.nan
  1190. health_status[["Internet_Purpose_news"]] = np.nan
  1191. health_status[["Internet_Purpose_videos"]] = np.nan
  1192. health_status[["Internet_Purpose_games"]] = np.nan
  1193. health_status[["Internet_Purpose_Financial"]] = np.nan
  1194. health_status[["Internet_Purpose_Others"]] = np.nan
  1195. # 是否会用手机支付
  1196. health_status[["Mobile_Payment"]] = np.nan
  1197. # 是否使用微信?
  1198. health_status[["Wechat_Usage"]] = np.nan
  1199. # 发不发微信朋友圈?
  1200. health_status[["Post_Moments"]] = np.nan
  1201. # 现在还在吸烟还是戒烟了?
  1202. # 1 仍然抽烟
  1203. # 2 戒烟
  1204. health_status['Current_Smoking_Status']=health_status["da061"].apply(lambda x : 1 if x == 1 else 2 if x ==2 else np.nan)
  1205. # 更新一下2013年没有回答的用户
  1206. health_status["da061_w3"] = health_status["da061_w3"].apply(lambda x : 1 if x ==1 else 2 if x ==2 else np.nan)
  1207. data_2013 = pd.merge(data_2013, health_status[['ID',"da061_w3"]], on='ID', how='left')
  1208. data_2013['Current_Smoking_Status'] = data_2013['Current_Smoking_Status'].fillna(data_2013['da061_w3'])
  1209. data_2013 = data_2013.drop('da061_w3', axis=1)
  1210. # 按 'ID' 列合并两个表
  1211. # 吸烟时,一般抽什么烟?
  1212. # (1) Smoking a pipe 用烟管吸烟(烟袋、旱烟)
  1213. # (2) Smoking self-rolled cigarettes 自己卷烟抽
  1214. # (3) Filtered cigarette带滤咀香烟
  1215. # (4) Unfiltered cigarette不带滤咀香烟
  1216. # (5) Cigar雪茄
  1217. # (6) Water cigarettes 水烟
  1218. health_status['Smoking_Type_pipe']=health_status["da060s1"]
  1219. health_status['Smoking_Type_rolled']=health_status["da060s2"]
  1220. health_status['Smoking_Type_Filtered']=health_status["da060s3"]
  1221. health_status['Smoking_Type_Unfiltered']=health_status["da060s4"]
  1222. health_status['Smoking_Type_Cigar']=health_status["da060s5"]
  1223. health_status['Smoking_Type_Water']=health_status["da060s6"]
  1224. # 现在/戒烟前平均一天抽多少支香烟?
  1225. health_status['Daily_Cigarette_Count']=health_status["da063"]
  1226. # 在过去的一年, 喝酒吗
  1227. # (1) Drink more than once a month. 喝酒,每月超过一次
  1228. # (2) Drink but less than once a month 喝酒,但每月少于一次
  1229. # (3) None of these 什么都不喝
  1230. health_status['Drink_PastYear']=health_status["da067"]
  1231. # 过去一年内 平均一个月喝几次酒
  1232. # (1)Once a month 每月一次
  1233. # (2)2-3 times a month 每月2-3次
  1234. # (3)Once a week 每周一次
  1235. # (4)2-3 times a week 每周2-3次
  1236. # (5)4-6 times a week 每周4-6次
  1237. # (6)Once a day 每天一次
  1238. # (7)Twice a day 一天两次
  1239. # (8)More than twice a day 一天超过两次
  1240. health_status['Drink_Monthly_Frequency']=health_status.apply(lambda x : 8 if x["da072"] ==8 or x["da074"] ==8 or x["da076"] ==8 else
  1241. 7 if x["da072"] ==7 or x["da074"] ==7 or x["da076"] ==7 else
  1242. 6 if x["da072"] ==6 or x["da074"] ==6 or x["da076"] ==6 else
  1243. 5 if x["da072"] ==5 or x["da074"] ==5 or x["da076"] ==5 else
  1244. 4 if x["da072"] ==4 or x["da074"] ==4 or x["da076"] ==4 else
  1245. 3 if x["da072"] ==3 or x["da074"] ==3 or x["da076"] ==3 else
  1246. 2 if x["da072"] ==2 or x["da074"] ==2 or x["da076"] ==2 else
  1247. 1 if x["da072"] ==1 or x["da074"] ==1 or x["da076"] ==1 else np.nan, axis=1)
  1248. # 自上次访问以来的两年内,您是否发作过心脏病?
  1249. # 1 是
  1250. # 0 否
  1251. health_status["Heart_attack_2_years"] = health_status.apply(lambda x : 1 if x["da007_w2_5"] ==1 else
  1252. 0 if x["da007_w2_5"] == 2 else np.nan, axis=1)
  1253. # 自上次访问以来,是否有医生诊断您中风复发?
  1254. # 1 是
  1255. # 0 否
  1256. health_status['Recurrent_Stroke']=health_status.apply(lambda x : 1 if x["da019_w2_1"] ==1 else
  1257. 0 if x["da019_w2_1"] == 2 else np.nan, axis=1)
  1258. # 合并2013年的慢性病
  1259. columns_to_diseases_old = ['da007_1_', 'da007_2_','da007_3_','da007_4_','da007_5_','da007_6_','da007_7_','da007_8_','da007_9_','da007_10_','da007_11_'
  1260. ,'da007_12_','da007_13_','da007_14_']
  1261. columns_to_diseases_new = ['Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1262. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1263. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']
  1264. for (col_old, col_new) in zip(columns_to_diseases_old,columns_to_diseases_new):
  1265. health_status[col_new] = health_status.apply(lambda x : x[col_old] if not pd.isna(x[col_old]) else np.nan, axis=1)
  1266. diseases_2013 = data_2013[data_2013["wave"]=="2013"][['ID','Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1267. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1268. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']]
  1269. # 按 'ID' 列合并两个表
  1270. health_status = pd.merge(health_status, diseases_2013, on='ID', how='left', suffixes=("_2015","_2013"))
  1271. # 使用 fillna() 来更新字段
  1272. for col in columns_to_diseases_new:
  1273. health_status[col] = health_status[f'{col}_2015'].fillna(health_status[f'{col}_2013'])
  1274. health_status_select = health_status[['ID','householdID', 'communityID', 'Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1275. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1276. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma', "Physical_activity", "Smoke", "Drink", "Accident_Or_Injury", "Fell_In_Last2Years", "Menarche_Year", "Menarche_Age",
  1277. "Menopause_Year", "Menopause_Age", "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses" ,
  1278. "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  1279. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  1280. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  1281. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  1282. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  1283. , "da056s1", "da056s2", "da056s3", "da056s4", "da056s5", "da056s6", "da056s7", "da056s8", "da056s9"
  1284. , "da057_1_" , "da057_2_", "da057_3_", "da057_4_", "da057_5_", "da057_6_", "da057_7_", "da057_8_"
  1285. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  1286. , "Internet_Purpose_Chat", "Internet_Purpose_news", "Internet_Purpose_videos", "Internet_Purpose_games", "Internet_Purpose_Financial", "Internet_Purpose_Others"
  1287. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  1288. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  1289. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency", "Heart_attack_2_years", "Recurrent_Stroke"]]
  1290. data_2015 = pd.merge(data_2015, health_status_select, on = ["ID", 'householdID', 'communityID'], how="left")
  1291. #计算认知功能得分,分成三部分:电话问卷9分,词语回忆10分、画图1分
  1292. health_status["dc001s1_score"] = health_status["dc001s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  1293. health_status["dc001s2_score"] = health_status["dc001s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  1294. health_status["dc001s3_score"] = health_status["dc001s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  1295. health_status["dc002_score"] = health_status["dc002"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  1296. # health_status["dc003_score"] = health_status["dc003"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  1297. health_status["dc019_score"] = health_status["dc019"].apply(lambda x : 1 if x==93 else 0 if pd.isna(x) else 0)
  1298. health_status["dc020_score"] = health_status["dc020"].apply(lambda x : 1 if x==86 else 0 if pd.isna(x) else 0)
  1299. health_status["dc021_score"] = health_status["dc021"].apply(lambda x : 1 if x==79 else 0 if pd.isna(x) else 0)
  1300. health_status["dc022_score"] = health_status["dc022"].apply(lambda x : 1 if x==72 else 0 if pd.isna(x) else 0)
  1301. health_status["dc023_score"] = health_status["dc023"].apply(lambda x : 1 if x==65 else 0 if pd.isna(x) else 0)
  1302. #词语记忆
  1303. health_status["dc006s1_score"] = health_status["dc006s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  1304. health_status["dc006s2_score"] = health_status["dc006s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  1305. health_status["dc006s3_score"] = health_status["dc006s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  1306. health_status["dc006s4_score"] = health_status["dc006s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  1307. health_status["dc006s5_score"] = health_status["dc006s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  1308. health_status["dc006s6_score"] = health_status["dc006s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  1309. health_status["dc006s7_score"] = health_status["dc006s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  1310. health_status["dc006s8_score"] = health_status["dc006s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  1311. health_status["dc006s9_score"] = health_status["dc006s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  1312. health_status["dc006s10_score"] = health_status["dc006s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  1313. # health_status["dc006s11_score"] = health_status["dc006s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  1314. health_status["dc027s1_score"] = health_status["dc027s1"].apply(lambda x : 1 if x==1 else 0 if pd.isna(x) else 0)
  1315. health_status["dc027s2_score"] = health_status["dc027s2"].apply(lambda x : 1 if x==2 else 0 if pd.isna(x) else 0)
  1316. health_status["dc027s3_score"] = health_status["dc027s3"].apply(lambda x : 1 if x==3 else 0 if pd.isna(x) else 0)
  1317. health_status["dc027s4_score"] = health_status["dc027s4"].apply(lambda x : 1 if x==4 else 0 if pd.isna(x) else 0)
  1318. health_status["dc027s5_score"] = health_status["dc027s5"].apply(lambda x : 1 if x==5 else 0 if pd.isna(x) else 0)
  1319. health_status["dc027s6_score"] = health_status["dc027s6"].apply(lambda x : 1 if x==6 else 0 if pd.isna(x) else 0)
  1320. health_status["dc027s7_score"] = health_status["dc027s7"].apply(lambda x : 1 if x==7 else 0 if pd.isna(x) else 0)
  1321. health_status["dc027s8_score"] = health_status["dc027s8"].apply(lambda x : 1 if x==8 else 0 if pd.isna(x) else 0)
  1322. health_status["dc027s9_score"] = health_status["dc027s9"].apply(lambda x : 1 if x==9 else 0 if pd.isna(x) else 0)
  1323. health_status["dc027s10_score"] = health_status["dc027s10"].apply(lambda x : 1 if x==10 else 0 if pd.isna(x) else 0)
  1324. # health_status["dc027s11_score"] = health_status["dc027s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  1325. #画图
  1326. health_status["draw_score"] = health_status["dc025"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  1327. data_2015["Cognition_score"] = health_status["dc001s1_score"] + health_status["dc001s2_score"] + \
  1328. health_status["dc001s3_score"] + health_status["dc002_score"]+ \
  1329. health_status["dc019_score"]+ health_status["dc020_score"] + health_status["dc021_score"]+ \
  1330. health_status["dc022_score"]+ health_status["dc023_score"] + health_status["dc006s1_score"] + \
  1331. health_status["dc006s2_score"] + health_status["dc006s3_score"] + health_status["dc006s4_score"] + \
  1332. health_status["dc006s5_score"] + health_status["dc006s6_score"] + health_status["dc006s7_score"] + \
  1333. health_status["dc006s8_score"] + health_status["dc006s9_score"] + health_status["dc006s10_score"] + \
  1334. health_status["dc027s1_score"]+ health_status["dc027s2_score"]+ \
  1335. health_status["dc027s3_score"]+ health_status["dc027s4_score"]+ health_status["dc027s5_score"]+ \
  1336. health_status["dc027s6_score"]+ health_status["dc027s7_score"]+ health_status["dc027s8_score"]+ \
  1337. health_status["dc027s9_score"]+health_status["dc027s10_score"]+\
  1338. health_status["draw_score"]
  1339. #心理得分
  1340. health_status["dc009_score"] = health_status["dc009"]-1
  1341. health_status["dc010_score"] = health_status["dc010"]-1
  1342. health_status["dc011_score"] = health_status["dc011"]-1
  1343. health_status["dc012_score"] = health_status["dc012"]-1
  1344. health_status["dc013_score"] = 4 - health_status["dc013"]
  1345. health_status["dc014_score"] = health_status["dc014"]-1
  1346. health_status["dc015_score"] = health_status["dc015"]-1
  1347. health_status["dc016_score"] = 4 - health_status["dc016"]
  1348. health_status["dc017_score"] = health_status["dc017"]-1
  1349. health_status["dc018_score"] = health_status["dc018"]-1
  1350. data_2015["psychiatric_score"] = health_status["dc009_score"] + health_status["dc010_score"] + health_status["dc011_score"] + \
  1351. health_status["dc012_score"] + health_status["dc013_score"] + health_status["dc014_score"] + health_status["dc015_score"] + \
  1352. health_status["dc016_score"] + health_status["dc017_score"] + health_status["dc018_score"]
  1353. #睡眠状态
  1354. # (1)Rarely or none of the time (<1 day) 很少或者根本没有(<1天)
  1355. # (2)Some or a little of the time (1-2 days) 不太多(1-2天)
  1356. # (3)Occasionally or a moderate amount of the time (3-4 days) 有时或者说有一半的时间(3-4天)
  1357. # (4)Most or all of the time (5-7 days) 大多数的时间(5-7天)
  1358. data_2015["sleep_state"] = health_status['dc015']
  1359. #ADL
  1360. health_status["db010_score"] = health_status["db010"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1361. health_status["db011_score"] = health_status["db011"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1362. health_status["db012_score"] = health_status["db012"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1363. health_status["db013_score"] = health_status["db013"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1364. health_status["db014_score"] = health_status["db014"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1365. health_status["db015_score"] = health_status["db015"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1366. data_2015["ADL"] = health_status["db010_score"] + health_status["db011_score"] + health_status["db012_score"] + health_status["db013_score"] + \
  1367. health_status["db014_score"] + health_status["db015_score"]
  1368. # 是否有管道煤气或天然气?
  1369. houseing["Gas_Connection"] = houseing["i019"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  1370. # 是否带供暖设施(不包括土暖气和可制暖的空调)?
  1371. houseing["Heating_Facility"] = houseing["i020"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  1372. # 供暖所用的主要能源是什么?
  1373. # (1)Solar 太阳能
  1374. # (2)Coal 煤炭、蜂窝煤
  1375. # (3)Natural gas 管道天然气或煤气
  1376. # (4)Liquefied Petroleum Gas 液化石油气
  1377. # (5)Electric 电
  1378. # (6)Crop residue/Wood buring 秸秆、柴火
  1379. # (7)Other 其他
  1380. houseing["Heating_Energy"] = houseing["i021"]
  1381. # 做饭用的主要燃料是什么?
  1382. # (1)Coal 煤炭、蜂窝煤
  1383. # (2)Natural gas 管道天然气或煤气
  1384. # (3)Marsh gas 沼气
  1385. # (4)Liquefied Petroleum Gas 液化石油气
  1386. # (5)Electric 电
  1387. # (6)crop residue/Wood burning 秸秆、柴火
  1388. # (7)other 其他
  1389. houseing["Cooking_Fuel"] = houseing["i022"]
  1390. houseing_select = houseing[['ID','householdID', 'communityID','Gas_Connection',
  1391. 'Heating_Facility', 'Heating_Energy', 'Cooking_Fuel']]
  1392. data_2015 = pd.merge(data_2015, houseing_select, on = ["ID", 'householdID', 'communityID'], how="left")
  1393. data_2015["wave"] = year
  1394. change_columns(data_2015)
  1395. data_2015 = pd.merge(data_2015, demo, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_demo'))
  1396. data_2015 = pd.merge(data_2015, psu[["communityID", "areatype"]], on = ['communityID'], how="left", suffixes=('', '_psu'))
  1397. data_2015 = pd.merge(data_2015, biomarkers, on = ["ID", "householdID", "communityID"], how="left", suffixes=('', '_bio'))
  1398. data_2015 = pd.merge(data_2015, family_information, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_information'))
  1399. data_2015 = pd.merge(data_2015, family_transfer, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_transfer'))
  1400. data_2015 = pd.merge(data_2015, household_income, on = ['householdID', 'communityID'], how="left", suffixes=('', '_incomeh'))
  1401. # data_2015 = pd.merge(data_2015, Household_Member, on = ['householdID', 'communityID'], how="left", suffixes=('', '_memberh'))
  1402. data_2015 = pd.merge(data_2015, health_status, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_statush'))
  1403. data_2015 = pd.merge(data_2015, health_care, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_careh'))
  1404. data_2015 = pd.merge(data_2015, exp_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomee'))
  1405. data_2015 = pd.merge(data_2015, weight, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_weightw'))
  1406. data_2015 = pd.merge(data_2015, houseing, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_houseing'))
  1407. # data_2015 = pd.merge(data_2015, child, on = ['communityID'], how="left", suffixes=('', '_child'))
  1408. data_2015 = pd.merge(data_2015, individual_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomei'))
  1409. data_2015 = pd.merge(data_2015, Sample_Infor, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Sample'))
  1410. data_2015 = pd.merge(data_2015, work_retirement_and_pension, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_pension'))
  1411. # data_2015 = pd.merge(data_2015, Sibling, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Sibling'))
  1412. # data_2015 = pd.merge(data_2015, Spousal_Sibling, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Spousal'))
  1413. #修改地区名称
  1414. #省份、城市名称和污染物数据格式对齐
  1415. #海东地区->海东市
  1416. data_2015['city'] = data_2015['city'].replace('海东地区', '海东市')
  1417. #北京 -> 北京市
  1418. data_2015['city'] = data_2015['city'].replace('北京', '北京市')
  1419. data_2015['province'] = data_2015['province'].replace('北京', '北京市')
  1420. #哈尔滨 -> 哈尔滨市
  1421. data_2015['city'] = data_2015['city'].replace('哈尔滨', '哈尔滨市')
  1422. #天津 -> 天津市
  1423. data_2015['city'] = data_2015['city'].replace('天津', '天津市')
  1424. data_2015['province'] = data_2015['province'].replace('天津', '天津市')
  1425. #广西省 -> 广西壮族自治区
  1426. data_2015['province'] = data_2015['province'].replace('广西省', '广西壮族自治区')
  1427. #巢湖市 -> 合肥市
  1428. data_2015['city'] = data_2015['city'].replace('巢湖市', '合肥市')
  1429. #襄樊市->襄阳市
  1430. data_2015['city'] = data_2015['city'].replace('襄樊市', '襄阳市')
  1431. # data_2015 = pd.concat([data_2013, data_2015], axis=0)
  1432. print("2015 complete")
  1433. # 2018年
  1434. year = "2018"
  1435. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Demographic_Background.dta")
  1436. psu, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS2013/PSU.dta", encoding='gbk')
  1437. health_status, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Status_and_Functioning.dta")
  1438. health_care, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Care_and_Insurance.dta")
  1439. cognition, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Cognition.dta")
  1440. Sample_Infor, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Sample_Infor.dta")
  1441. houseing, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Housing.dta")
  1442. family_information, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Information.dta")
  1443. family_transfer, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Transfer.dta")
  1444. household_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Household_Income.dta")
  1445. individual_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Individual_Income.dta")
  1446. Insider, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Insider.dta")
  1447. weight, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Weights.dta")
  1448. Work_Retirement, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Work_Retirement.dta")
  1449. Pension, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Pension.dta")
  1450. #性别#年龄#婚姻状况
  1451. # 1 married or partnered
  1452. # 0 other marital status (separated, divorced, unmarried, or widowed)
  1453. demo["marital_status"] = demo.apply(lambda x : 1 if x["be001"]==1 or x["be001"]==2 or x["be002"]==1 else 0 if x["be001"] in [3,4,5,6] else np.nan, axis=1)
  1454. #教育
  1455. # 0 below high school
  1456. # 1 high school
  1457. # 2 college or above
  1458. #更新2015的教育
  1459. demo["education"] = demo.apply(lambda x : x["bd001_w2_4"] if not pd.isna(x["bd001_w2_4"]) else np.nan, axis=1)
  1460. demo["education"] = demo["education"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  1461. # 出生年
  1462. demo["birth_year"] = demo.apply(lambda x : x["ba004_w3_1"] if x["ba005_w4"]==1 else x["ba002_1"] if x["ba005_w4"]==2 else np.nan, axis=1)
  1463. demo["birth_month"] = demo.apply(lambda x : x["ba004_w3_2"] if x["ba005_w4"]==1 else x["ba002_2"] if x["ba005_w4"]==2 else np.nan, axis=1)
  1464. #获取随访时间
  1465. demo = pd.merge(demo, Sample_Infor[["ID", "iyear", "imonth"]], on = "ID", how="left")
  1466. data_2018 = demo[['ID','householdID', 'communityID','xrgender', 'birth_year','birth_month','ba003',"iyear", "imonth", 'marital_status', 'education']]
  1467. #居住地
  1468. # 0 农村
  1469. # 1 城市
  1470. data_2018 = pd.merge(data_2018, psu[['communityID', 'province', 'city', 'urban_nbs']], on = "communityID", how="left")
  1471. #身高#体重#收缩压#舒张压
  1472. data_2018[['qi002', 'ql002', 'waist','qa011' ,'qa012']]=np.nan
  1473. #受试者可以在不用手臂支撑的情况下按其平时的节奏连续起立坐下五次吗
  1474. data_2018[["Sit_Stand_5x_yes","Sit_Stand_5x_no", "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2"]] = np.nan
  1475. #白细胞(WBC),平均红血球容积MCV,血小板,血尿素氮bun,葡萄糖glu,血肌酐crea,总胆固醇cho,甘油三酯tg,高密度脂蛋白HDL,低密度脂蛋白胆固醇LDL,C反应蛋白CRP
  1476. #糖化血红蛋白hba1c,尿酸ua,血细胞比容Hematocrit,血红蛋白hgb,胱抑素C
  1477. data_2018[['bl_wbc','bl_mcv','bl_plt','bl_bun','bl_glu','bl_crea','bl_cho', 'bl_tg', 'bl_hdl', 'bl_ldl','bl_crp','bl_hbalc','bl_ua', 'bl_hct', 'bl_hgb','bl_cysc']]=np.nan
  1478. # 慢性病:
  1479. # (1) Hypertension 高血压病
  1480. # (2) Dyslipidemia (elevation of low density lipoprotein, triglycerides (TGs),and total cholesterol, or a low high density lipoprotein level)血脂异常(包括低密度脂蛋白、甘油三酯、总胆固醇的升高或(和)高密度脂蛋白的下降)
  1481. # (3) Diabetes or high blood sugar糖尿病或血糖升高(包括糖耐量异常和空腹血糖升高)
  1482. # (4) Cancer or malignant tumor (excluding minor skin cancers) 癌症等恶性肿瘤(不包括轻度皮肤癌)
  1483. # (5) Chronic lung diseases, such as chronic bronchitis , emphysema ( excluding tumors, or cancer) 慢性肺部疾患如慢性支气管炎或肺气肿、肺心病(不包括肿瘤或癌)
  1484. # (6) Liver disease (except fatty liver, tumors, and cancer) 肝脏疾病
  1485. # (除脂肪肝、肿瘤或癌外)
  1486. # (7) Heart attack, coronary heart disease, angina, congestive heart failure, or other heart problems 心脏病(如心肌梗塞、冠心病、心绞痛、充血性心力衰竭和其他心脏疾病)
  1487. # (8) Stroke 中风
  1488. # (9) Kidney disease (except for tumor or cancer) 肾脏疾病(不包括肿瘤或癌)
  1489. # (10) Stomach or other digestive disease (except for tumor or cancer) 胃部疾病或消化系统疾病(不包括肿瘤或癌)
  1490. # (11) Emotional, nervous, or psychiatric problems 情感及精神方面问题
  1491. # (12) Memory-related disease 与记忆相关的疾病 (如老年痴呆症、脑萎缩、帕金森症)
  1492. # (13) Arthritis or rheumatism 关节炎或风湿病
  1493. # (14) Asthma 哮喘
  1494. # 体力活动
  1495. # 2 vigorous (vigorous activity more than once a week)
  1496. # 1 moderate (moderate activity more than once a week)
  1497. # 0 inactive (the rest)
  1498. health_status["Physical_activity"] = health_status.apply(lambda x : 2 if x["da051_1_"]==1 else
  1499. 1 if x["da051_2_"]==1 else
  1500. 0 if x["da051_3_"] == 1 or (x["da051_1_"]==2 and x["da051_2_"]==2 and x["da051_3_"] == 2)
  1501. else np.nan ,axis=1)
  1502. # 抽烟
  1503. # 1 抽过烟
  1504. # 0 没有抽过烟
  1505. health_status["Smoke"] = health_status["da059"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else 1)
  1506. # 喝酒
  1507. # 1 喝过酒
  1508. # 0 没有喝过酒
  1509. health_status["Drink"] = health_status.apply(lambda x : 1 if x["da067"] ==1 or x["da067"] ==2 else
  1510. 0 if x["da069"] == 1 else
  1511. 1 if x["da069"] == 2 or x["da069"] == 3 else np.nan, axis=1)
  1512. # 您是否经历过交通事故,或任何的重大意外伤害,并接受了治疗?
  1513. # 1 是
  1514. # 0 否
  1515. health_status['Accident_Or_Injury']=health_status.apply(lambda x : 1 if (not pd.isna(x["da021"]) and x["da021"]==1) or (pd.isna(x["da021"]) and not pd.isna(x["da022"]) )else
  1516. 0 if (not pd.isna(x["da021"]) and x["da021"]==2) or (pd.isna(x["da021"]) and pd.isna(x["da022"]) ) else np.nan, axis=1)
  1517. # 过去两年有没有摔倒?
  1518. # 1 是
  1519. # 0 否
  1520. health_status['Fell_In_Last2Years']=health_status.apply(lambda x : 1 if x["da023"] ==1 or x["da023_w4"]==1 else
  1521. 0 if x["da023"] ==2 or x["da023_w4"]==2 else np.nan, axis=1)
  1522. # 您什么时候开始来月经的?(year/age)
  1523. health_status['Menarche_Year']=health_status["da026_1"]
  1524. health_status['Menarche_Age']=health_status["da026_2"]
  1525. # 您什么时候开始绝经的?
  1526. health_status['Menopause_Year']=health_status["da028_1"]
  1527. health_status['Menopause_Age']=health_status["da028_2"]
  1528. # 第一次诊断出您有前列腺疾病是在什么时候?
  1529. health_status['Prostate_Issue_Year']=health_status["da030_1"]
  1530. health_status['Prostate_Issue_Age']=health_status["da030_2"]
  1531. # 是否戴眼镜(包括矫正视力镜片)?
  1532. # 1 是
  1533. # 0 否
  1534. # 2 失明
  1535. # 3 偶尔
  1536. health_status['Wear_Glasses']=health_status["da032"].apply(lambda x : 1 if x == 1 else 2 if x ==2 else 0 if x == 3 else 3 if x == 4 else np.nan)
  1537. # 过去一个月内,您平均每天晚上真正睡着的时间大约是几小时?(可能短于您在床上躺着的时间)
  1538. health_status['Average_Sleep_Hours']=health_status["da049"]
  1539. # 过去一个月内,您通常午睡多长时间?分钟
  1540. health_status['Average_Nap_Minutes']=health_status["da050"]
  1541. # 您通常每周有没有至少持续做激烈活动十分钟?
  1542. health_status['Vigorous_Activity_10Min']=health_status["da051_1_"]
  1543. # 您通常每周有没有至少持续做中等强度的体力活动十分钟?
  1544. health_status['Moderate_Effort_10Min']=health_status["da051_2_"]
  1545. # 您通常每周有没有至少持续走路十分钟?
  1546. health_status['Walking_10Min']=health_status["da051_3_"]
  1547. # 您通常每周有多少天做[激烈活动]至少十分钟?
  1548. health_status['Vigorous_Activity_Days']=health_status["da052_1_"]
  1549. # 您通常每周有多少天做[中等强度的体力活动]至少十分钟?
  1550. health_status['Moderate_Effort_Days']=health_status["da052_2_"]
  1551. # 您通常每周有多少天做[走路]至少十分钟?
  1552. health_status['Walking_Days']=health_status["da052_3_"]
  1553. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 2小时
  1554. health_status['Vigorous_Activity_2Hours_PerDay']=health_status["da053_1_"]
  1555. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 2小时
  1556. health_status['Moderate_Effort_2Hours_PerDay']=health_status["da053_2_"]
  1557. # 在做[走路]的这些天里,您一天花多少时间做[走路] 2小时
  1558. health_status['Walking_2Hours_PerDay']=health_status["da053_3_"]
  1559. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 30分钟
  1560. health_status['Vigorous_Activity_30Min_PerDay']=health_status["da054_1_"]
  1561. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 30分钟
  1562. health_status['Moderate_Effort_30Min_PerDay']=health_status["da054_2_"]
  1563. # 在做[走路]的这些天里,您一天花多少时间做[走路] 30分钟
  1564. health_status['Walking_30Min_PerDay']=health_status["da054_3_"]
  1565. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 4小时
  1566. health_status['Vigorous_Activity_4Hours_PerDay']=health_status["da055_1_"]
  1567. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 4小时
  1568. health_status['Moderate_Effort_4Hours_PerDay']=health_status["da055_2_"]
  1569. # 在做[走路]的这些天里,您一天花多少时间做[走路] 4小时
  1570. health_status['Walking_4Hours_PerDay']=health_status["da055_3_"]
  1571. # 活动的原因
  1572. # 1 工作需要
  1573. # 2 娱乐
  1574. # 3 体育锻炼
  1575. # 4 其他
  1576. health_status["Reason_For_Vigorous_Activity"]= health_status["da051_1_1_"]
  1577. health_status["Reason_For_Moderate_Effort"]= health_status["da051_1_2_"]
  1578. health_status["Reason_For_Walking"]= health_status["da051_1_3_"]
  1579. # 过去一个月是否进行了下列社交活动?
  1580. # (1) 串门、跟朋友交往
  1581. # (2) 打麻将、下棋、打牌、去社区活动室
  1582. # (3) 无偿向与您不住在一起的亲人、朋友或者邻居提供帮助
  1583. # (4) 去公园或者其他场所跳舞、健身、练气功等
  1584. # (5) 参加社团组织活动
  1585. # (6) 志愿者活动或者慈善活动/无偿照顾与您不住在一起的病人或残疾人
  1586. # (7) 上学或者参加培训课程
  1587. # (8)其他
  1588. # (9) 以上均没有
  1589. health_status["da056_s1"] = health_status.apply(lambda x: 1 if x["da056_s1"]==1 else 0, axis=1)
  1590. health_status["da056_s2"] = health_status.apply(lambda x: 2 if x["da056_s2"]==2 else 0, axis=1)
  1591. health_status["da056_s3"] = health_status.apply(lambda x: 3 if x["da056_s3"]==3 else 0, axis=1)
  1592. health_status["da056_s4"] = health_status.apply(lambda x: 4 if x["da056_s4"]==4 else 0, axis=1)
  1593. health_status["da056_s5"] = health_status.apply(lambda x: 5 if x["da056_s5"]==5 else 0, axis=1)
  1594. health_status["da056_s6"] = health_status.apply(lambda x: 6 if x["da056_s6"]==6 or x["da056_s7"]==7 else 0, axis=1)
  1595. health_status["da056_s7"] = health_status.apply(lambda x: 7 if x["da056_s8"]==8 else 0, axis=1)
  1596. health_status["da056_s8"] = health_status.apply(lambda x: 8 if x["da056_s9"]==9 or x["da056_s10"]==10 or x["da056_s11"]==11 else 0, axis=1)
  1597. health_status["da056_s9"] = health_status.apply(lambda x: 7 if x["da056_s12"]==12 else 0, axis=1)
  1598. # 过去一个月的活动频率
  1599. # (1) Almost daily 差不多每天
  1600. # (2) Almost every week 差不多每周
  1601. # (3) Not regularly 不经常
  1602. health_status["da057_6_"] = health_status.apply(lambda x: 1 if x["da057_6_"]==1 or x["da057_7_"]==1 else 2 if x["da057_6_"]==2 or x["da057_7_"]==2 else 3 if x["da057_6_"]==3 or x["da057_7_"]==3 else np.nan, axis=1)
  1603. health_status["da057_7_"] = health_status["da057_8_"]
  1604. health_status["da057_8_"] = health_status.apply(lambda x: 1 if x["da057_9_"]==1 or x["da057_10_"]==1 or x["da057_11_"]==1
  1605. else 2 if x["da057_9_"]==2 or x["da057_10_"]==2 or x["da057_11_"]==2
  1606. else 3 if x["da057_9_"]==3 or x["da057_10_"]==3 or x["da057_11_"]==3
  1607. else np.nan, axis=1)
  1608. # 过去一个月,您是否上网?
  1609. health_status["Internet_Usage_LastMonth"] = health_status["da056_s10"].apply(lambda x : 1 if x==10 else 0)
  1610. # 使用以下哪些工具上网?
  1611. # 1. Desktop computer 台式电脑
  1612. # 2. Laptop computer 笔记本电脑
  1613. # 3. Tablet computer 平板电脑(如 IPAD)
  1614. # 4. Cellphone 手机
  1615. # 5. Other devices 其他设备
  1616. health_status["Internet_Tools_Desktop_computer"] = health_status["da056_w3_s1"]
  1617. health_status["Internet_Tools_Laptop_computer"] = health_status["da056_w3_s2"]
  1618. health_status["Internet_Tools_Tablet_computer"] = health_status["da056_w3_s3"]
  1619. health_status["Internet_Tools_Cellphone"] = health_status["da056_w3_s4"]
  1620. health_status["Internet_Tools_Other"] = health_status["da056_w3_s5"]
  1621. # 上网一般做什么?
  1622. health_status["Internet_Purpose_Chat"] = health_status["da056_w4_1_s1"]
  1623. health_status["Internet_Purpose_news"] = health_status["da056_w4_1_s2"]
  1624. health_status["Internet_Purpose_videos"] = health_status["da056_w4_1_s3"]
  1625. health_status["Internet_Purpose_games"] = health_status["da056_w4_1_s4"]
  1626. health_status["Internet_Purpose_Financial"] = health_status["da056_w4_1_s5"]
  1627. health_status["Internet_Purpose_Others"] = health_status["da056_w4_1_s6"]
  1628. # 是否会用手机支付
  1629. # 1 是
  1630. # 0 否
  1631. health_status["Mobile_Payment"] = health_status["da056_w4_2"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  1632. # 是否使用微信?
  1633. health_status["Wechat_Usage"] = health_status["da056_w4_3"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  1634. # 发不发微信朋友圈?
  1635. health_status["Post_Moments"] = health_status["da056_w4_4"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  1636. # 现在还在吸烟还是戒烟了?
  1637. # 1 仍然抽烟
  1638. # 2 戒烟
  1639. health_status['Current_Smoking_Status']=health_status.apply(lambda x : 1 if x["da061"] == 1 or x["da061_w4"] == 1 else 2 if x["da061"] == 2 or x["da061_w4"] == 2 else np.nan, axis=1)
  1640. # 吸烟时,一般抽什么烟?
  1641. # (1) Smoking a pipe 用烟管吸烟(烟袋、旱烟)
  1642. # (2) Smoking self-rolled cigarettes 自己卷烟抽
  1643. # (3) Filtered cigarette带滤咀香烟
  1644. # (4) Unfiltered cigarette不带滤咀香烟
  1645. # (5) Cigar雪茄
  1646. # (6) Water cigarettes 水烟
  1647. health_status.loc[health_status['da060'] == 1, 'Smoking_Type_pipe'] = 1
  1648. health_status.loc[health_status['da060'] == 2, 'Smoking_Type_rolled'] = 2
  1649. health_status.loc[health_status['da060'] == 3, 'Smoking_Type_Filtered'] = 3
  1650. health_status.loc[health_status['da060'] == 4, 'Smoking_Type_Unfiltered'] = 4
  1651. health_status.loc[health_status['da060'] == 5, 'Smoking_Type_Cigar'] = 5
  1652. health_status.loc[health_status['da060'] == 6, 'Smoking_Type_Water'] = 6
  1653. # 现在/戒烟前平均一天抽多少支香烟?
  1654. health_status['Daily_Cigarette_Count']=health_status["da063"]
  1655. # 在过去的一年, 喝酒吗
  1656. # (1) Drink more than once a month. 喝酒,每月超过一次
  1657. # (2) Drink but less than once a month 喝酒,但每月少于一次
  1658. # (3) None of these 什么都不喝
  1659. health_status['Drink_PastYear']=health_status["da067"]
  1660. # 过去一年内 平均一个月喝几次酒
  1661. # (1)Once a month 每月一次
  1662. # (2)2-3 times a month 每月2-3次
  1663. # (3)Once a week 每周一次
  1664. # (4)2-3 times a week 每周2-3次
  1665. # (5)4-6 times a week 每周4-6次
  1666. # (6)Once a day 每天一次
  1667. # (7)Twice a day 一天两次
  1668. # (8)More than twice a day 一天超过两次
  1669. health_status['Drink_Monthly_Frequency']=health_status.apply(lambda x : 8 if x["da072"] ==8 or x["da074"] ==8 or x["da076"] ==8 else
  1670. 7 if x["da072"] ==7 or x["da074"] ==7 or x["da076"] ==7 else
  1671. 6 if x["da072"] ==6 or x["da074"] ==6 or x["da076"] ==6 else
  1672. 5 if x["da072"] ==5 or x["da074"] ==5 or x["da076"] ==5 else
  1673. 4 if x["da072"] ==4 or x["da074"] ==4 or x["da076"] ==4 else
  1674. 3 if x["da072"] ==3 or x["da074"] ==3 or x["da076"] ==3 else
  1675. 2 if x["da072"] ==2 or x["da074"] ==2 or x["da076"] ==2 else
  1676. 1 if x["da072"] ==1 or x["da074"] ==1 or x["da076"] ==1 else np.nan, axis=1)
  1677. # 自上次访问以来的两年内,您是否发作过心脏病?
  1678. # 1 是
  1679. # 0 否
  1680. health_status["Heart_attack_2_years"] = health_status.apply(lambda x : 1 if x["da007_w2_5"] ==1 else
  1681. 0 if x["da007_w2_5"] == 2 else np.nan, axis=1)
  1682. # 自上次访问以来,是否有医生诊断您中风复发?
  1683. # 1 是
  1684. # 0 否
  1685. health_status['Recurrent_Stroke']=health_status.apply(lambda x : 1 if x["da019_w2_1"] ==1 else
  1686. 0 if x["da019_w2_1"] == 2 else np.nan, axis=1)
  1687. columns_to_diseases_old = ['da007_1_', 'da007_2_','da007_3_','da007_4_','da007_5_','da007_6_','da007_7_','da007_8_','da007_9_','da007_10_','da007_11_'
  1688. ,'da007_12_','da007_13_','da007_14_']
  1689. columns_to_diseases_new = ['Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1690. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1691. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']
  1692. for (col_old, col_new) in zip(columns_to_diseases_old,columns_to_diseases_new):
  1693. health_status[col_new] = health_status.apply(lambda x : x[col_old] if not pd.isna(x[col_old]) else np.nan, axis=1)
  1694. diseases_2015 = data_2015[data_2015["wave"]=="2015"][['ID','Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1695. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1696. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']]
  1697. # 按 'ID' 列合并两个表
  1698. health_status = pd.merge(health_status, diseases_2015, on='ID', how='left', suffixes=("_2018","_2015"))
  1699. # 使用 fillna() 来更新字段
  1700. for col in columns_to_diseases_new:
  1701. health_status[col] = health_status[f'{col}_2018'].fillna(health_status[f'{col}_2015'])
  1702. health_status_select = health_status[['ID','householdID', 'communityID', 'Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  1703. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  1704. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma', "Physical_activity", "Smoke", "Drink", "Accident_Or_Injury", "Fell_In_Last2Years", "Menarche_Year", "Menarche_Age",
  1705. "Menopause_Year", "Menopause_Age", "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses" ,
  1706. "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  1707. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  1708. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  1709. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  1710. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  1711. , "da056_s1", "da056_s2", "da056_s3", "da056_s4", "da056_s5", "da056_s6", "da056_s7", "da056_s8", "da056_s9"
  1712. , "da057_1_" , "da057_2_", "da057_3_", "da057_4_", "da057_5_", "da057_6_", "da057_7_", "da057_8_"
  1713. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  1714. , "Internet_Purpose_Chat", "Internet_Purpose_news", "Internet_Purpose_videos", "Internet_Purpose_games", "Internet_Purpose_Financial", "Internet_Purpose_Others"
  1715. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  1716. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  1717. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency","Heart_attack_2_years", "Recurrent_Stroke"]]
  1718. data_2018 = pd.merge(data_2018, health_status_select, on = ["ID", 'householdID', 'communityID'], how="left")
  1719. #计算认知功能得分,分成三部分:电话问卷9分,词语回忆10分、画图1分
  1720. cognition["dc001s1_score"] = cognition["dc001_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1721. cognition["dc001s2_score"] = cognition["dc006_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1722. cognition["dc001s3_score"] = cognition["dc003_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1723. cognition["dc002_score"] = cognition["dc005_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1724. # cognition["dc003_score"] = cognition["dc002_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1725. cognition["dc019_score"] = cognition.apply(lambda x : 0 if x["dc014_w4_1"]==97 else 1 if pd.isna(x["dc014_w4_1"]) and x["dc014_w4_1_1"]==93 else 0 if pd.isna(x["dc014_w4_1"]) and (not x["dc014_w4_1_1"]==93) else np.nan, axis=1)
  1726. cognition["dc020_score"] = cognition.apply(lambda x : 0 if x["dc014_w4_2"]==97 else 1 if pd.isna(x["dc014_w4_2"]) and x["dc014_w4_2_1"]==86 else 0 if pd.isna(x["dc014_w4_2"]) and (not x["dc014_w4_2_1"]==86) else np.nan, axis=1)
  1727. cognition["dc021_score"] = cognition.apply(lambda x : 0 if x["dc014_w4_3"]==97 else 1 if pd.isna(x["dc014_w4_3"]) and x["dc014_w4_3_1"]==79 else 0 if pd.isna(x["dc014_w4_3"]) and (not x["dc014_w4_3_1"]==79) else np.nan, axis=1)
  1728. cognition["dc022_score"] = cognition.apply(lambda x : 0 if x["dc014_w4_4"]==97 else 1 if pd.isna(x["dc014_w4_4"]) and x["dc014_w4_4_1"]==72 else 0 if pd.isna(x["dc014_w4_4"]) and (not x["dc014_w4_4_1"]==72) else np.nan, axis=1)
  1729. cognition["dc023_score"] = cognition.apply(lambda x : 0 if x["dc014_w4_5"]==97 else 1 if pd.isna(x["dc014_w4_5"]) and x["dc014_w4_5_1"]==65 else 0 if pd.isna(x["dc014_w4_5"]) and (not x["dc014_w4_5_1"]==65) else np.nan, axis=1)
  1730. #词语记忆
  1731. cognition["dc006s1_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s1"]==1 else 0, axis=1)
  1732. cognition["dc006s2_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s2"]==2 else 0, axis=1)
  1733. cognition["dc006s3_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s3"]==3 else 0, axis=1)
  1734. cognition["dc006s4_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s4"]==4 else 0, axis=1)
  1735. cognition["dc006s5_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s5"]==5 else 0, axis=1)
  1736. cognition["dc006s6_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s6"]==6 else 0, axis=1)
  1737. cognition["dc006s7_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s7"]==7 else 0, axis=1)
  1738. cognition["dc006s8_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s8"]==8 else 0, axis=1)
  1739. cognition["dc006s9_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s9"]==9 else 0, axis=1)
  1740. cognition["dc006s10_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc028_w4_s10"]==10 else 0, axis=1)
  1741. # cognition["dc006s11_score"] = cognition["dc028_w4_s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  1742. cognition["dc027s1_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s1"]==1 else 0, axis=1)
  1743. cognition["dc027s2_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s2"]==2 else 0, axis=1)
  1744. cognition["dc027s3_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s3"]==3 else 0, axis=1)
  1745. cognition["dc027s4_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s4"]==4 else 0, axis=1)
  1746. cognition["dc027s5_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s5"]==5 else 0, axis=1)
  1747. cognition["dc027s6_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s6"]==6 else 0, axis=1)
  1748. cognition["dc027s7_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s7"]==7 else 0, axis=1)
  1749. cognition["dc027s8_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s8"]==8 else 0, axis=1)
  1750. cognition["dc027s9_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s9"]==9 else 0, axis=1)
  1751. cognition["dc027s10_score"] = cognition.apply(lambda x : np.nan if not x["wr101_intro"] ==1 else 1 if x["dc047_w4_s10"]==10 else 0, axis=1)
  1752. # cognition["dc027s11_score"] = cognition["dc047_w4_s11"].apply(lambda x : 1 if x==11 else 0 if pd.isna(x) else 0)
  1753. #画图
  1754. cognition["draw_score"] = cognition["dc024_w4"].apply(lambda x : 1 if x==1 else 0 if x==5 else np.nan)
  1755. data_2018["Cognition_score"] = cognition["dc001s1_score"] + cognition["dc001s2_score"] + \
  1756. cognition["dc001s3_score"] + cognition["dc002_score"]+ \
  1757. cognition["dc019_score"]+ cognition["dc020_score"] + cognition["dc021_score"]+ \
  1758. cognition["dc022_score"]+ cognition["dc023_score"] + cognition["dc006s1_score"] + \
  1759. cognition["dc006s2_score"] + cognition["dc006s3_score"] + cognition["dc006s4_score"] + \
  1760. cognition["dc006s5_score"] + cognition["dc006s6_score"] + cognition["dc006s7_score"] + \
  1761. cognition["dc006s8_score"] + cognition["dc006s9_score"] + cognition["dc006s10_score"] + \
  1762. cognition["dc027s1_score"]+ cognition["dc027s2_score"]+ \
  1763. cognition["dc027s3_score"]+ cognition["dc027s4_score"]+ cognition["dc027s5_score"]+ \
  1764. cognition["dc027s6_score"]+ cognition["dc027s7_score"]+ cognition["dc027s8_score"]+ \
  1765. cognition["dc027s9_score"]+cognition["dc027s10_score"]+\
  1766. cognition["draw_score"]
  1767. #心理得分
  1768. cognition["dc009_score"] = cognition["dc009"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1769. cognition["dc010_score"] = cognition["dc010"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1770. cognition["dc011_score"] = cognition["dc011"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1771. cognition["dc012_score"] = cognition["dc012"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1772. cognition["dc013_score"] = cognition["dc013"].apply(lambda x: 4-x if (not pd.isna(x)) and x <5 else np.nan)
  1773. cognition["dc014_score"] = cognition["dc014"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1774. cognition["dc015_score"] = cognition["dc015"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1775. cognition["dc016_score"] = cognition["dc016"].apply(lambda x: 4-x if (not pd.isna(x)) and x <5 else np.nan)
  1776. cognition["dc017_score"] = cognition["dc017"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1777. cognition["dc018_score"] = cognition["dc018"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  1778. data_2018["psychiatric_score"] = cognition["dc009_score"] + cognition["dc010_score"] + cognition["dc011_score"] + \
  1779. cognition["dc012_score"] + cognition["dc013_score"] + cognition["dc014_score"] + cognition["dc015_score"] + \
  1780. cognition["dc016_score"] + cognition["dc017_score"] + cognition["dc018_score"]
  1781. #睡眠状态
  1782. # (1)Rarely or none of the time (<1 day) 很少或者根本没有(<1天)
  1783. # (2)Some or a little of the time (1-2 days) 不太多(1-2天)
  1784. # (3)Occasionally or a moderate amount of the time (3-4 days) 有时或者说有一半的时间(3-4天)
  1785. # (4)Most or all of the time (5-7 days) 大多数的时间(5-7天)
  1786. data_2018["sleep_state"] = cognition['dc015'].apply(lambda x : np.nan if x > 4 else x)
  1787. #ADL
  1788. health_status["db010_score"] = health_status["db010"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1789. health_status["db011_score"] = health_status["db011"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1790. health_status["db012_score"] = health_status["db012"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1791. health_status["db013_score"] = health_status["db013"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1792. health_status["db014_score"] = health_status["db014"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1793. health_status["db015_score"] = health_status["db015"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  1794. data_2018["ADL"] = health_status["db010_score"] + health_status["db011_score"] + health_status["db012_score"] + health_status["db013_score"] + \
  1795. health_status["db014_score"] + health_status["db015_score"]
  1796. # 是否有管道煤气或天然气?
  1797. houseing["Gas_Connection"] = houseing["i019"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  1798. # 是否带供暖设施(不包括土暖气和可制暖的空调)?
  1799. houseing["Heating_Facility"] = houseing["i020"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  1800. # 供暖所用的主要能源是什么?
  1801. # (1)Solar 太阳能
  1802. # (2)Coal 煤炭、蜂窝煤
  1803. # (3)Natural gas 管道天然气或煤气
  1804. # (4)Liquefied Petroleum Gas 液化石油气
  1805. # (5)Electric 电
  1806. # (6)Crop residue/Wood buring 秸秆、柴火
  1807. # (7)Other 其他
  1808. houseing["Heating_Energy"] = houseing["i021_w4"].apply(lambda x : 7 if x==8 else x )
  1809. # 做饭用的主要燃料是什么?
  1810. # (1)Coal 煤炭、蜂窝煤
  1811. # (2)Natural gas 管道天然气或煤气
  1812. # (3)Marsh gas 沼气
  1813. # (4)Liquefied Petroleum Gas 液化石油气
  1814. # (5)Electric 电
  1815. # (6)crop residue/Wood burning 秸秆、柴火
  1816. # (7)other 其他
  1817. houseing["Cooking_Fuel"] = houseing["i022_w4"].apply(lambda x : np.nan if x==8 else x )
  1818. houseing_select = houseing[['householdID', 'communityID','Gas_Connection',
  1819. 'Heating_Facility', 'Heating_Energy', 'Cooking_Fuel']]
  1820. data_2018 = pd.merge(data_2018, houseing_select, on = ['householdID', 'communityID'], how="left")
  1821. data_2018["wave"] = year
  1822. change_columns(data_2018)
  1823. data_2018 = pd.merge(data_2018, demo, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_demo'))
  1824. data_2018 = pd.merge(data_2018, psu[["communityID", "areatype"]], on = ['communityID'], how="left", suffixes=('', '_psu'))
  1825. data_2018 = pd.merge(data_2018, health_status, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_statush'))
  1826. data_2018 = pd.merge(data_2018, health_care, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_careh'))
  1827. data_2018 = pd.merge(data_2018, cognition, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_cognition'))
  1828. data_2018 = pd.merge(data_2018, Sample_Infor, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Sample'))
  1829. data_2018 = pd.merge(data_2018, houseing, on = ['householdID', 'communityID'], how="left", suffixes=('', '_houseing'))
  1830. data_2018 = pd.merge(data_2018, family_information, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_information'))
  1831. data_2018 = pd.merge(data_2018, family_transfer, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_transfer'))
  1832. data_2018 = pd.merge(data_2018, household_income, on = ['householdID', 'communityID'], how="left", suffixes=('', '_incomeh'))
  1833. data_2018 = pd.merge(data_2018, individual_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomei'))
  1834. data_2018 = pd.merge(data_2018, Insider, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Insider'))
  1835. data_2018 = pd.merge(data_2018, weight, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_weightw'))
  1836. data_2018 = pd.merge(data_2018, Work_Retirement, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Retirement'))
  1837. data_2018 = pd.merge(data_2018, Pension, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Pension'))
  1838. #修改地区名称
  1839. #省份、城市名称和污染物数据格式对齐
  1840. #海东地区->海东市
  1841. data_2018['city'] = data_2018['city'].replace('海东地区', '海东市')
  1842. #北京 -> 北京市
  1843. data_2018['city'] = data_2018['city'].replace('北京', '北京市')
  1844. data_2018['province'] = data_2018['province'].replace('北京', '北京市')
  1845. #哈尔滨 -> 哈尔滨市
  1846. data_2018['city'] = data_2018['city'].replace('哈尔滨', '哈尔滨市')
  1847. #天津 -> 天津市
  1848. data_2018['city'] = data_2018['city'].replace('天津', '天津市')
  1849. data_2018['province'] = data_2018['province'].replace('天津', '天津市')
  1850. #广西省 -> 广西壮族自治区
  1851. data_2018['province'] = data_2018['province'].replace('广西省', '广西壮族自治区')
  1852. #巢湖市 -> 合肥市
  1853. data_2018['city'] = data_2018['city'].replace('巢湖市', '合肥市')
  1854. #襄樊市->襄阳市
  1855. data_2018['city'] = data_2018['city'].replace('襄樊市', '襄阳市')
  1856. # data_2018 = pd.concat([data_2015, data_2018], axis=0)
  1857. print("2018 complete")
  1858. # 2020年
  1859. year = "2020"
  1860. demo, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Demographic_Background.dta")
  1861. psu, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS2013/PSU.dta", encoding='gbk')
  1862. health_status, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Health_Status_and_Functioning.dta")
  1863. Sample_Infor, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Sample_Infor.dta")
  1864. houseing, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Household_Income.dta")
  1865. COVID_Module, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/COVID_Module.dta")
  1866. Exit_Module, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Exit_Module.dta")
  1867. family_information, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Family_Information.dta")
  1868. individual_income, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Individual_Income.dta")
  1869. weight, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Weights.dta")
  1870. Work_Retirement, meta = pyreadstat.read_dta("/root/r_base/CHARLS/CHARLS"+year+"/Work_Retirement.dta")
  1871. #性别#年龄#婚姻状况
  1872. # 1 married or partnered
  1873. # 0 other marital status (separated, divorced, unmarried, or widowed)
  1874. demo["marital_status"] = demo.apply(lambda x : 1 if x["ba011"]==1 or x["ba011"]==2 or x["ba012"]==1 else 0 if x["ba011"] in [3,4,5,6] else np.nan, axis=1)
  1875. #教育
  1876. # 0 below high school
  1877. # 1 high school
  1878. # 2 college or above
  1879. demo["education"] = demo.apply(lambda x : x["ba010"] if not pd.isna(x["ba010"]) else np.nan, axis=1)
  1880. demo["education"] = demo["education"].apply(lambda x : 1 if x == 6 or x == 7 else 2 if x in [8, 9, 10, 11] else 0 if x in [1,2,3,4,5] else np.nan)
  1881. #合并2018年的教育
  1882. eductaion_2018 = data_2018[data_2018["wave"]=="2018"][['ID',"education"]]
  1883. # 按 'ID' 列合并两个表
  1884. demo = pd.merge(demo, eductaion_2018, on='ID', how='left', suffixes=("_2020","_2018"))
  1885. # 使用 fillna() 来更新字段
  1886. demo['education'] = demo['education_2020'].fillna(demo['education_2018'])
  1887. # 出生年
  1888. demo["birth_year"] = demo.apply(lambda x : x["ba003_1"] if pd.isna(x["ba003_1"]) else np.nan, axis=1)
  1889. demo["birth_month"] = demo.apply(lambda x : x["ba003_2"] if pd.isna(x["ba003_2"]) else np.nan, axis=1)
  1890. #合并2018年的出生年
  1891. birth_year_2018 = data_2018[data_2018["wave"]=="2018"][['ID',"birth_year", "birth_month"]]
  1892. # 按 'ID' 列合并两个表
  1893. demo = pd.merge(demo, birth_year_2018, on='ID', how='left', suffixes=("_2020","_2018"))
  1894. # 使用 fillna() 来更新字段
  1895. demo['birth_year'] = demo['birth_year_2020'].fillna(demo['birth_year_2018'])
  1896. demo['birth_month'] = demo['birth_month_2020'].fillna(demo['birth_month_2018'])
  1897. #获取随访时间
  1898. demo = pd.merge(demo, Sample_Infor[["ID", "iyear", "imonth"]], on = "ID", how="left")
  1899. demo["ba003"] = 1
  1900. data_2020 = demo[['ID','householdID', 'communityID','xrgender', 'birth_year','birth_month','ba003',"iyear", "imonth", 'marital_status', 'education']]
  1901. #居住地
  1902. # 0 农村
  1903. # 1 城市
  1904. data_2020 = pd.merge(data_2020, psu[['communityID', 'province', 'city', 'urban_nbs']], on = "communityID", how="left")
  1905. #身高#体重#收缩压#舒张压
  1906. data_2020[['qi002', 'ql002', 'waist', 'Systolic','Diastolic']]=np.nan
  1907. #受试者可以在不用手臂支撑的情况下按其平时的节奏连续起立坐下五次吗
  1908. data_2020[["Sit_Stand_5x_yes","Sit_Stand_5x_no", "Sit_Stand_5x_times", "Walking_Speed_Time_1", "Walking_Speed_Time_2"]] = np.nan
  1909. #白细胞(WBC),平均红血球容积MCV,血小板,血尿素氮bun,葡萄糖glu,血肌酐crea,总胆固醇cho,甘油三酯tg,高密度脂蛋白HDL,低密度脂蛋白胆固醇LDL,C反应蛋白CRP
  1910. #糖化血红蛋白hba1c,尿酸ua,血细胞比容Hematocrit,血红蛋白hgb,胱抑素C
  1911. data_2020[['bl_wbc','bl_mcv','bl_plt','bl_bun','bl_glu','bl_crea','bl_cho', 'bl_tg', 'bl_hdl', 'bl_ldl','bl_crp','bl_hbalc','bl_ua', 'bl_hct', 'bl_hgb','bl_cysc']]=np.nan
  1912. # 慢性病:
  1913. # (1) Hypertension 高血压病
  1914. # (2) Dyslipidemia (elevation of low density lipoprotein, triglycerides (TGs),and total cholesterol, or a low high density lipoprotein level)血脂异常(包括低密度脂蛋白、甘油三酯、总胆固醇的升高或(和)高密度脂蛋白的下降)
  1915. # (3) Diabetes or high blood sugar糖尿病或血糖升高(包括糖耐量异常和空腹血糖升高)
  1916. # (4) Cancer or malignant tumor (excluding minor skin cancers) 癌症等恶性肿瘤(不包括轻度皮肤癌)
  1917. # (5) Chronic lung diseases, such as chronic bronchitis , emphysema ( excluding tumors, or cancer) 慢性肺部疾患如慢性支气管炎或肺气肿、肺心病(不包括肿瘤或癌)
  1918. # (6) Liver disease (except fatty liver, tumors, and cancer) 肝脏疾病
  1919. # (除脂肪肝、肿瘤或癌外)
  1920. # (7) Heart attack, coronary heart disease, angina, congestive heart failure, or other heart problems 心脏病(如心肌梗塞、冠心病、心绞痛、充血性心力衰竭和其他心脏疾病)
  1921. # (8) Stroke 中风
  1922. # (9) Kidney disease (except for tumor or cancer) 肾脏疾病(不包括肿瘤或癌)
  1923. # (10) Stomach or other digestive disease (except for tumor or cancer) 胃部疾病或消化系统疾病(不包括肿瘤或癌)
  1924. # (11) Emotional, nervous, or psychiatric problems 情感及精神方面问题
  1925. # (12) Memory-related disease 与记忆相关的疾病 (如老年痴呆症、脑萎缩、帕金森症)
  1926. # (13) Arthritis or rheumatism 关节炎或风湿病
  1927. # (14) Asthma 哮喘
  1928. # 2020年把帕金森和记忆病症分开,需要和以前对齐
  1929. # 体力活动
  1930. # 2 vigorous (vigorous activity more than once a week)
  1931. # 1 moderate (moderate activity more than once a week)
  1932. # 0 inactive (the rest)
  1933. health_status["Physical_activity"] = health_status.apply(lambda x : 2 if x["da032_1_"]==1 else
  1934. 1 if x["da032_2_"]==1 else
  1935. 0 if x["da032_3_"] == 1 or (x["da032_1_"]==2 and x["da032_2_"]==2 and x["da032_3_"] == 2)
  1936. else np.nan ,axis=1)
  1937. # 抽烟
  1938. # 1 抽过烟
  1939. # 0 没有抽过烟
  1940. health_status["Smoke"] = health_status["da046"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else 1)
  1941. # 喝酒
  1942. # 1 喝过酒
  1943. # 0 没有喝过酒
  1944. health_status["Drink"] = health_status.apply(lambda x : 1 if x["da051"] ==1 or x["da051"] ==2 else
  1945. 0 if x["da051"] == 3 else np.nan, axis=1)
  1946. # 您是否经历过交通事故,或任何的重大意外伤害,并接受了治疗?
  1947. # 1 是
  1948. # 0 否
  1949. health_status['Accident_Or_Injury']=health_status.apply(lambda x : 1 if x["da019"] ==1 or x["da020"]==1 else
  1950. 0 if x["da019"] ==2 or x["da020"]==2 else np.nan, axis=1)
  1951. # 过去两年有没有摔倒?
  1952. # 1 是
  1953. # 0 否
  1954. health_status['Fell_In_Last2Years']=health_status.apply(lambda x : 1 if x["da022"] ==1 or x["da023"]==1 else
  1955. 0 if x["da022"] ==2 or x["da023"]==2 else np.nan, axis=1)
  1956. # 您什么时候开始来月经的?(year/age)
  1957. health_status[['Menarche_Year']]=np.nan
  1958. health_status[['Menarche_Age']]=np.nan
  1959. # 您什么时候开始绝经的?
  1960. health_status[['Menopause_Year']]=np.nan
  1961. health_status[['Menopause_Age']]=np.nan
  1962. # 第一次诊断出您有前列腺疾病是在什么时候?
  1963. health_status[['Prostate_Issue_Year']]=np.nan
  1964. health_status[['Prostate_Issue_Age']]=np.nan
  1965. # 是否戴眼镜(包括矫正视力镜片)?
  1966. # 1 是
  1967. # 0 否
  1968. # 2 失明
  1969. # 3 偶尔
  1970. health_status[['Wear_Glasses']]=np.nan
  1971. # 过去一个月内,您平均每天晚上真正睡着的时间大约是几小时?(可能短于您在床上躺着的时间)
  1972. health_status['Average_Sleep_Hours']=health_status["da030"]
  1973. # 过去一个月内,您通常午睡多长时间?分钟
  1974. health_status['Average_Nap_Minutes']=health_status["da031"]
  1975. # 您通常每周有没有至少持续做激烈活动十分钟?
  1976. health_status['Vigorous_Activity_10Min']=health_status["da032_1_"]
  1977. # 您通常每周有没有至少持续做中等强度的体力活动十分钟?
  1978. health_status['Moderate_Effort_10Min']=health_status["da032_2_"]
  1979. # 您通常每周有没有至少持续走路十分钟?
  1980. health_status['Walking_10Min']=health_status["da032_3_"]
  1981. # 您通常每周有多少天做[激烈活动]至少十分钟?
  1982. health_status['Vigorous_Activity_Days']=health_status["da033_1_"]
  1983. # 您通常每周有多少天做[中等强度的体力活动]至少十分钟?
  1984. health_status['Moderate_Effort_Days']=health_status["da033_2_"]
  1985. # 您通常每周有多少天做[走路]至少十分钟?
  1986. health_status['Walking_Days']=health_status["da033_3_"]
  1987. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 2小时
  1988. health_status['Vigorous_Activity_2Hours_PerDay']=health_status["da034_1_"]
  1989. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 2小时
  1990. health_status['Moderate_Effort_2Hours_PerDay']=health_status["da034_2_"]
  1991. # 在做[走路]的这些天里,您一天花多少时间做[走路] 2小时
  1992. health_status['Walking_2Hours_PerDay']=health_status["da034_3_"]
  1993. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 30分钟
  1994. health_status['Vigorous_Activity_30Min_PerDay']=health_status["da035_1_"]
  1995. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 30分钟
  1996. health_status['Moderate_Effort_30Min_PerDay']=health_status["da035_2_"]
  1997. # 在做[走路]的这些天里,您一天花多少时间做[走路] 30分钟
  1998. health_status['Walking_30Min_PerDay']=health_status["da035_3_"]
  1999. # 在做[激烈活动]的这些天里,您一天花多少时间做[激烈活动] 4小时
  2000. health_status['Vigorous_Activity_4Hours_PerDay']=health_status["da036_1_"]
  2001. # 在做[中等强度的体力活动]的这些天里,您一天花多少时间做[中等强度的体力活动] 4小时
  2002. health_status['Moderate_Effort_4Hours_PerDay']=health_status["da036_2_"]
  2003. # 在做[走路]的这些天里,您一天花多少时间做[走路] 4小时
  2004. health_status['Walking_4Hours_PerDay']=health_status["da036_3_"]
  2005. # 活动的原因
  2006. # 1 工作需要
  2007. # 2 娱乐
  2008. # 3 体育锻炼
  2009. # 4 其他
  2010. health_status["Reason_For_Vigorous_Activity"]= health_status["da037_1_"]
  2011. health_status["Reason_For_Moderate_Effort"]= health_status["da037_2_"]
  2012. health_status["Reason_For_Walking"]= health_status["da037_3_"]
  2013. # 过去一个月是否进行了下列社交活动?
  2014. # (1) 串门、跟朋友交往
  2015. # (2) 打麻将、下棋、打牌、去社区活动室
  2016. # (3) 无偿向与您不住在一起的亲人、朋友或者邻居提供帮助
  2017. # (4) 去公园或者其他场所跳舞、健身、练气功等
  2018. # (5) 参加社团组织活动
  2019. # (6) 志愿者活动或者慈善活动/无偿照顾与您不住在一起的病人或残疾人
  2020. # (7) 上学或者参加培训课程
  2021. # (8)其他
  2022. # (9) 以上均没有
  2023. # 过去一个月的活动频率
  2024. # (1) Almost daily 差不多每天
  2025. # (2) Almost every week 差不多每周
  2026. # (3) Not regularly 不经常
  2027. # 过去一个月,您是否上网?
  2028. # 1 是
  2029. # 0 否
  2030. health_status["Internet_Usage_LastMonth"] = health_status["da040"].apply(lambda x : 1 if x ==1 else 0)
  2031. # 使用以下哪些工具上网?
  2032. # 1. Desktop computer 台式电脑
  2033. # 2. Laptop computer 笔记本电脑
  2034. # 3. Tablet computer 平板电脑(如 IPAD)
  2035. # 4. Cellphone 手机
  2036. # 5. Other devices 其他设备
  2037. health_status["Internet_Tools_Desktop_computer"] = health_status["da041_s1"]
  2038. health_status["Internet_Tools_Laptop_computer"] = health_status["da041_s2"]
  2039. health_status["Internet_Tools_Tablet_computer"] = health_status["da041_s3"]
  2040. health_status["Internet_Tools_Cellphone"] = health_status["da041_s4"]
  2041. health_status["Internet_Tools_Other"] = health_status["da041_s5"]
  2042. # 上网一般做什么?
  2043. health_status["Internet_Purpose_Chat"] = health_status["da042_s1"]
  2044. health_status["Internet_Purpose_news"] = health_status["da042_s2"]
  2045. health_status["Internet_Purpose_videos"] = health_status["da042_s3"]
  2046. health_status["Internet_Purpose_games"] = health_status["da042_s4"]
  2047. health_status["Internet_Purpose_Financial"] = health_status["da042_s5"]
  2048. health_status["Internet_Purpose_Others"] = health_status["da042_s6"]
  2049. # 是否会用手机支付
  2050. # 1 是
  2051. # 0 否
  2052. health_status["Mobile_Payment"] = health_status["da043"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2053. # 是否使用微信?
  2054. health_status["Wechat_Usage"] = health_status["da044"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2055. # 发不发微信朋友圈?
  2056. health_status["Post_Moments"] = health_status["da045"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2057. # 现在还在吸烟还是戒烟了?
  2058. # 1 仍然抽烟
  2059. # 2 戒烟
  2060. health_status['Current_Smoking_Status']=health_status.apply(lambda x : 1 if x["da047"] == 1 else 2 if x["da047"] == 2 else np.nan, axis=1)
  2061. # 吸烟时,一般抽什么烟?
  2062. # (1) Smoking a pipe 用烟管吸烟(烟袋、旱烟)
  2063. # (2) Smoking self-rolled cigarettes 自己卷烟抽
  2064. # (3) Filtered cigarette带滤咀香烟
  2065. # (4) Unfiltered cigarette不带滤咀香烟
  2066. # (5) Cigar雪茄
  2067. # (6) Water cigarettes 水烟
  2068. health_status.loc[health_status['da048'] == 1, 'Smoking_Type_pipe'] = 1
  2069. health_status.loc[health_status['da048'] == 2, 'Smoking_Type_rolled'] = 2
  2070. health_status.loc[health_status['da048'] == 3, 'Smoking_Type_Filtered'] = 3
  2071. health_status.loc[health_status['da048'] == 4, 'Smoking_Type_Unfiltered'] = 4
  2072. health_status.loc[health_status['da048'] == 5, 'Smoking_Type_Cigar'] = 5
  2073. health_status.loc[health_status['da048'] == 6, 'Smoking_Type_Water'] = 6
  2074. # 现在/戒烟前平均一天抽多少支香烟?
  2075. health_status['Daily_Cigarette_Count']=health_status.apply(lambda x : x["da050_1"] if not pd.isna(x["da050_1"]) else x["da050_2"] if not pd.isna(x["da050_2"]) else np.nan, axis=1)
  2076. # 在过去的一年, 喝酒吗
  2077. # (1) Drink more than once a month. 喝酒,每月超过一次
  2078. # (2) Drink but less than once a month 喝酒,但每月少于一次
  2079. # (3) None of these 什么都不喝
  2080. health_status['Drink_PastYear']=health_status["da051"]
  2081. # 过去一年内 平均一个月喝几次酒
  2082. # (1)Once a month 每月一次
  2083. # (2)2-3 times a month 每月2-3次
  2084. # (3)Once a week 每周一次
  2085. # (4)2-3 times a week 每周2-3次
  2086. # (5)4-6 times a week 每周4-6次
  2087. # (6)Once a day 每天一次
  2088. # (7)Twice a day 一天两次
  2089. # (8)More than twice a day 一天超过两次
  2090. health_status['Drink_Monthly_Frequency']=health_status["da052"]
  2091. health_status['da003_12_'] = health_status.apply(process_row, axis=1)
  2092. columns_to_diseases_old = ['da003_1_', 'da003_2_','da003_3_','da003_4_','da003_5_','da003_6_','da003_7_','da003_8_','da003_9_','da003_10_','da003_11_'
  2093. ,'da003_12_','da003_14_','da003_15_']
  2094. columns_to_diseases_new = ['Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  2095. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  2096. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']
  2097. for (col_old, col_new) in zip(columns_to_diseases_old,columns_to_diseases_new):
  2098. health_status[col_new] = health_status.apply(lambda x : x[col_old] if not pd.isna(x[col_old]) else np.nan, axis=1)
  2099. diseases_2018 = data_2018[data_2018["wave"]=="2018"][['ID','Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  2100. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  2101. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma']]
  2102. # 按 'ID' 列合并两个表
  2103. health_status = pd.merge(health_status, diseases_2018, on='ID', how='left', suffixes=("_2020","_2018"))
  2104. # 使用 fillna() 来更新字段
  2105. for col in columns_to_diseases_new:
  2106. health_status[col] = health_status[f'{col}_2020'].fillna(health_status[f'{col}_2018'])
  2107. health_status_select = health_status[['ID','householdID', 'communityID', 'Hypertension','Dyslipidemia','Disabetes_or_High_Blood_Sugar','Cancer_or_Malignant_Tumor','Chronic_Lung_Diseases',
  2108. 'Liver_Disease', 'Heart_Problems', 'Stroke', 'Kidney_Diease','Stomach_or_Other_Digestive_Disease',
  2109. 'Emotional_Nervous_or_Psychiatric_Problems', 'Memory_Related_Disease','Arthritis_or_Rheumatism','Asthma', "Physical_activity", "Smoke", "Drink", "Accident_Or_Injury", "Fell_In_Last2Years", "Menarche_Year", "Menarche_Age",
  2110. "Menopause_Year", "Menopause_Age", "Prostate_Issue_Year", "Prostate_Issue_Age", "Wear_Glasses",
  2111. "Average_Sleep_Hours", "Average_Nap_Minutes", "Vigorous_Activity_10Min", "Moderate_Effort_10Min"
  2112. , "Walking_10Min", "Vigorous_Activity_Days", "Moderate_Effort_Days", "Walking_Days", "Vigorous_Activity_2Hours_PerDay"
  2113. , "Moderate_Effort_2Hours_PerDay", "Walking_2Hours_PerDay", 'Vigorous_Activity_30Min_PerDay'
  2114. , "Moderate_Effort_30Min_PerDay", "Walking_30Min_PerDay", "Vigorous_Activity_4Hours_PerDay"
  2115. , "Moderate_Effort_4Hours_PerDay", "Walking_4Hours_PerDay", "Reason_For_Vigorous_Activity", "Reason_For_Moderate_Effort", "Reason_For_Walking"
  2116. , "da038_s1", "da038_s2", "da038_s3", "da038_s4", "da038_s5", "da038_s6", "da038_s7", "da038_s8", "da038_s9"
  2117. , "da039_1_" , "da039_2_", "da039_3_", "da039_4_", "da039_5_", "da039_6_", "da039_7_", "da039_8_"
  2118. , "Internet_Usage_LastMonth", "Internet_Tools_Desktop_computer","Internet_Tools_Laptop_computer", "Internet_Tools_Tablet_computer", "Internet_Tools_Cellphone", "Internet_Tools_Other"
  2119. , "Internet_Purpose_Chat", "Internet_Purpose_news", "Internet_Purpose_videos", "Internet_Purpose_games", "Internet_Purpose_Financial", "Internet_Purpose_Others"
  2120. , "Mobile_Payment", "Wechat_Usage", "Post_Moments", "Current_Smoking_Status"
  2121. , "Smoking_Type_pipe","Smoking_Type_rolled","Smoking_Type_Filtered","Smoking_Type_Unfiltered","Smoking_Type_Cigar","Smoking_Type_Water"
  2122. , "Daily_Cigarette_Count", "Drink_PastYear", "Drink_Monthly_Frequency",]]
  2123. data_2020 = pd.merge(data_2020, health_status_select, on = ["ID", 'householdID', 'communityID'], how="left")
  2124. # 自上次访问以来的两年内,您是否发作过心脏病?
  2125. # 1 是
  2126. # 0 否
  2127. data_2020[['Heart_attack_2_years']]=np.nan
  2128. # 自上次访问以来,是否有医生诊断您中风复发?
  2129. # 1 是
  2130. # 0 否
  2131. data_2020[['Recurrent_Stroke']]=np.nan
  2132. #计算认知功能得分,分成三部分:电话问卷9分,词语回忆10分、画图1分
  2133. health_status["dc001s1_score"] = health_status["dc001"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2134. health_status["dc001s2_score"] = health_status["dc005"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2135. health_status["dc001s3_score"] = health_status["dc003"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2136. health_status["dc002_score"] = health_status["dc004"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2137. health_status["dc003_score"] = health_status["dc002"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2138. health_status["dc019_score"] = health_status.apply(lambda x : 0 if x["dc007_1"]==997 else 1 if x["dc007_1"] ==1 and x["dc007_1_1"]==93 else 0 if x["dc007_1"] ==1 and (not x["dc007_1_1"]==93) else np.nan, axis=1)
  2139. health_status["dc020_score"] = health_status.apply(lambda x : 0 if x["dc007_2"]==997 else 1 if x["dc007_2"] ==1 and x["dc007_2_1"]==86 else 0 if x["dc007_2"] ==1 and (not x["dc007_2_1"]==86) else np.nan, axis=1)
  2140. health_status["dc021_score"] = health_status.apply(lambda x : 0 if x["dc007_3"]==997 else 1 if x["dc007_3"] ==1 and x["dc007_3_1"]==79 else 0 if x["dc007_3"] ==1 and (not x["dc007_3_1"]==79) else np.nan, axis=1)
  2141. health_status["dc022_score"] = health_status.apply(lambda x : 0 if x["dc007_4"]==997 else 1 if x["dc007_4"] ==1 and x["dc007_4_1"]==72 else 0 if x["dc007_4"] ==1 and (not x["dc007_4_1"]==72) else np.nan, axis=1)
  2142. health_status["dc023_score"] = health_status.apply(lambda x : 0 if x["dc007_5"]==997 else 1 if x["dc007_5"] ==1 and x["dc007_5_1"]==65 else 0 if x["dc007_5"] ==1 and (not x["dc007_5_1"]==65) else np.nan, axis=1)
  2143. #词语记忆
  2144. health_status["dc006s1_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s1"]==1 else 0, axis=1)
  2145. health_status["dc006s2_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s2"]==2 else 0, axis=1)
  2146. health_status["dc006s3_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s3"]==3 else 0, axis=1)
  2147. health_status["dc006s4_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s4"]==4 else 0, axis=1)
  2148. health_status["dc006s5_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s5"]==5 else 0, axis=1)
  2149. health_status["dc006s6_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s6"]==6 else 0, axis=1)
  2150. health_status["dc006s7_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s7"]==7 else 0, axis=1)
  2151. health_status["dc006s8_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s8"]==8 else 0, axis=1)
  2152. health_status["dc006s9_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s9"]==9 else 0, axis=1)
  2153. health_status["dc006s10_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc012_s10"]==10 else 0, axis=1)
  2154. health_status["dc027s1_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s1"]==1 else 0, axis=1)
  2155. health_status["dc027s2_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s2"]==2 else 0, axis=1)
  2156. health_status["dc027s3_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s3"]==3 else 0, axis=1)
  2157. health_status["dc027s4_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s4"]==4 else 0, axis=1)
  2158. health_status["dc027s5_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s5"]==5 else 0, axis=1)
  2159. health_status["dc027s6_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s6"]==6 else 0, axis=1)
  2160. health_status["dc027s7_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s7"]==7 else 0, axis=1)
  2161. health_status["dc027s8_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s8"]==8 else 0, axis=1)
  2162. health_status["dc027s9_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s9"]==9 else 0, axis=1)
  2163. health_status["dc027s10_score"] = health_status.apply(lambda x : np.nan if not x["xwordrecallbr"] ==1 else 1 if x["dc028_s10"]==10 else 0, axis=1)
  2164. #画图
  2165. health_status["draw_score"] = health_status["dc009"].apply(lambda x : 1 if x==1 else 0 if x==2 else np.nan)
  2166. data_2020["Cognition_score"] = health_status["dc001s1_score"] + health_status["dc001s2_score"] + \
  2167. health_status["dc001s3_score"] + health_status["dc002_score"]+ \
  2168. health_status["dc019_score"]+ health_status["dc020_score"] + health_status["dc021_score"]+ \
  2169. health_status["dc022_score"]+ health_status["dc023_score"] + health_status["dc006s1_score"] + \
  2170. health_status["dc006s2_score"] + health_status["dc006s3_score"] + health_status["dc006s4_score"] + \
  2171. health_status["dc006s5_score"] + health_status["dc006s6_score"] + health_status["dc006s7_score"] + \
  2172. health_status["dc006s8_score"] + health_status["dc006s9_score"] + health_status["dc006s10_score"] + \
  2173. health_status["dc027s1_score"]+ health_status["dc027s2_score"]+ \
  2174. health_status["dc027s3_score"]+ health_status["dc027s4_score"]+ health_status["dc027s5_score"]+ \
  2175. health_status["dc027s6_score"]+ health_status["dc027s7_score"]+ health_status["dc027s8_score"]+ \
  2176. health_status["dc027s9_score"]+health_status["dc027s10_score"]+\
  2177. health_status["draw_score"]
  2178. #心理得分
  2179. health_status["dc009_score"] = health_status["dc016"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2180. health_status["dc010_score"] = health_status["dc017"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2181. health_status["dc011_score"] = health_status["dc018"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2182. health_status["dc012_score"] = health_status["dc019"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2183. health_status["dc013_score"] = health_status["dc020"].apply(lambda x: 4-x if (not pd.isna(x)) and x <5 else np.nan)
  2184. health_status["dc014_score"] = health_status["dc021"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2185. health_status["dc015_score"] = health_status["dc022"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2186. health_status["dc016_score"] = health_status["dc023"].apply(lambda x: 4-x if (not pd.isna(x)) and x <5 else np.nan)
  2187. health_status["dc017_score"] = health_status["dc024"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2188. health_status["dc018_score"] = health_status["dc025"].apply(lambda x: x-1 if (not pd.isna(x)) and x <5 else np.nan)
  2189. data_2020["psychiatric_score"] = health_status["dc009_score"] + health_status["dc010_score"] + health_status["dc011_score"] + \
  2190. health_status["dc012_score"] + health_status["dc013_score"] + health_status["dc014_score"] + health_status["dc015_score"] + \
  2191. health_status["dc016_score"] + health_status["dc017_score"] + health_status["dc018_score"]
  2192. #睡眠状态
  2193. # (1)Rarely or none of the time (<1 day) 很少或者根本没有(<1天)
  2194. # (2)Some or a little of the time (1-2 days) 不太多(1-2天)
  2195. # (3)Occasionally or a moderate amount of the time (3-4 days) 有时或者说有一半的时间(3-4天)
  2196. # (4)Most or all of the time (5-7 days) 大多数的时间(5-7天)
  2197. data_2020["sleep_state"] = health_status['dc022'].apply(lambda x : np.nan if x >900 else x)
  2198. #ADL
  2199. health_status["db010_score"] = health_status["db001"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2200. health_status["db011_score"] = health_status["db003"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2201. health_status["db012_score"] = health_status["db005"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2202. health_status["db013_score"] = health_status["db007"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2203. health_status["db014_score"] = health_status["db009"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2204. health_status["db015_score"] = health_status["db011"].apply(lambda x : 0 if x==1 else 1 if x >= 2 else np.nan)
  2205. data_2020["ADL"] = health_status["db010_score"] + health_status["db011_score"] + health_status["db012_score"] + health_status["db013_score"] + \
  2206. health_status["db014_score"] + health_status["db015_score"]
  2207. # 是否有管道煤气或天然气?
  2208. houseing["Gas_Connection"] = houseing["i018"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  2209. # 是否带供暖设施(不包括土暖气和可制暖的空调)?
  2210. houseing["Heating_Facility"] = houseing["i019"].apply(lambda x : 1 if x ==1 else 0 if x == 2 else np.nan)
  2211. # 供暖所用的主要能源是什么?
  2212. # (1)Solar 太阳能
  2213. # (2)Coal 煤炭、蜂窝煤
  2214. # (3)Natural gas 管道天然气或煤气
  2215. # (4)Liquefied Petroleum Gas 液化石油气
  2216. # (5)Electric 电
  2217. # (6)Crop residue/Wood buring 秸秆、柴火
  2218. # (7)Other 其他
  2219. houseing["Heating_Energy"] = houseing["i020"].apply(lambda x : np.nan if x==8 else x )
  2220. # 做饭用的主要燃料是什么?
  2221. # (1)Coal 煤炭、蜂窝煤
  2222. # (2)Natural gas 管道天然气或煤气
  2223. # (3)Marsh gas 沼气
  2224. # (4)Liquefied Petroleum Gas 液化石油气
  2225. # (5)Electric 电
  2226. # (6)crop residue/Wood burning 秸秆、柴火
  2227. # (7)other 其他
  2228. houseing["Cooking_Fuel"] = houseing["i021"].apply(lambda x : np.nan if x==9 else 7 if x == 8 else x)
  2229. houseing_select = houseing[['householdID', 'communityID','Gas_Connection',
  2230. 'Heating_Facility', 'Heating_Energy', 'Cooking_Fuel']]
  2231. data_2020 = pd.merge(data_2020, houseing_select, on = ['householdID', 'communityID'], how="left")
  2232. data_2020["wave"] = year
  2233. change_columns(data_2020)
  2234. # data_2020 = pd.concat([data_2018, data_2020], axis=0)
  2235. data_2020 = pd.merge(data_2020, demo, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_demo'))
  2236. data_2020 = pd.merge(data_2020, psu[["communityID", "areatype"]], on = ['communityID'], how="left", suffixes=('', '_psu'))
  2237. data_2020 = pd.merge(data_2020, health_status, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_statush'))
  2238. # data_2020 = pd.merge(data_2020, health_care, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_careh'))
  2239. # data_2020 = pd.merge(data_2020, cognition, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_cognition'))
  2240. data_2020 = pd.merge(data_2020, Sample_Infor, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Sample'))
  2241. data_2020 = pd.merge(data_2020, houseing, on = ['householdID', 'communityID'], how="left", suffixes=('', '_houseing'))
  2242. data_2020 = pd.merge(data_2020, COVID_Module, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_COVID'))
  2243. data_2020 = pd.merge(data_2020, Exit_Module, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Exit'))
  2244. data_2020 = pd.merge(data_2020, family_information, on = ['householdID', 'communityID'], how="left", suffixes=('', '_information'))
  2245. # data_2020 = pd.merge(data_2020, family_transfer, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_transfer'))
  2246. # data_2020 = pd.merge(data_2020, household_income, on = ['householdID', 'communityID'], how="left", suffixes=('', '_incomeh'))
  2247. data_2020 = pd.merge(data_2020, individual_income, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_incomei'))
  2248. # data_2020 = pd.merge(data_2020, Insider, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Insider'))
  2249. data_2020 = pd.merge(data_2020, weight, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_weightw'))
  2250. data_2020 = pd.merge(data_2020, Work_Retirement, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Retirement'))
  2251. # data_2020 = pd.merge(data_2020, Pension, on = ["ID", 'householdID', 'communityID'], how="left", suffixes=('', '_Pension'))
  2252. #修改地区名称
  2253. #省份、城市名称和污染物数据格式对齐
  2254. #海东地区->海东市
  2255. data_2020['city'] = data_2020['city'].replace('海东地区', '海东市')
  2256. #北京 -> 北京市
  2257. data_2020['city'] = data_2020['city'].replace('北京', '北京市')
  2258. data_2020['province'] = data_2020['province'].replace('北京', '北京市')
  2259. #哈尔滨 -> 哈尔滨市
  2260. data_2020['city'] = data_2020['city'].replace('哈尔滨', '哈尔滨市')
  2261. #天津 -> 天津市
  2262. data_2020['city'] = data_2020['city'].replace('天津', '天津市')
  2263. data_2020['province'] = data_2020['province'].replace('天津', '天津市')
  2264. #广西省 -> 广西壮族自治区
  2265. data_2020['province'] = data_2020['province'].replace('广西省', '广西壮族自治区')
  2266. #巢湖市 -> 合肥市
  2267. data_2020['city'] = data_2020['city'].replace('巢湖市', '合肥市')
  2268. #襄樊市->襄阳市
  2269. data_2020['city'] = data_2020['city'].replace('襄樊市', '襄阳市')
  2270. data_2011.to_csv("2011_all_charls.csv", index=False)
  2271. data_2013.to_csv("2013_all_charls.csv", index=False)
  2272. data_2015.to_csv("2015_all_charls.csv", index=False)
  2273. data_2018.to_csv("2018_all_charls.csv", index=False)
  2274. data_2020.to_csv("2020_all_charls.csv", index=False)
  2275. # data_2020.to_csv("/root/r_base/CHARLS/result_all_new.csv", index=False)
  2276. print(123)