Как объединить несогласованные фреймы данных с pandas? - PullRequest
0 голосов

У меня есть четыре фрейма данных, которые я должен объединить в один. Данные представляют собой данные переписи населения с 2011 по 2014 год в Чили. Дело в том, что кадры данных представляют собой фактически одну и ту же информацию, но с некоторыми отличиями, которые немного усложняют (по крайней мере, для меня) процесс слияния. Мой код следующий:

import numpy as numpy                   
import pandas as pd                      
import matplotlib.pyplot as plt 
import seaborn as sn

col_2011=['region','death_code','death_cause','sex','< 1','1 a 4','5 a 9','10 a 19','20 a 44','45 a 64','65 a 79','80 y mas']
col_2012=['region','death_code','death_cause','sex','< 1','1 a 4','5 a 9','10 a 19','20 a 44','45 a 64','65 a 79','80 y mas']
col_2013=['region','death_code','death_cause','sex','0 a 4','5 a 9','10 a 14','15 a 19','20 a 24','25 a 29','30 a 34','35 a 39','40 a 44','45 a 49','50 a 54','55 a 59','60 a 64','65 a 69','70 a 74','75 a 79','80 y mas']
col_2014=['region','death_code','sex','0 a 4','5 a 9','10 a 14','15 a 19','20 a 24','25 a 29','30 a 34','35 a 39','40 a 44','45 a 49','50 a 54','55 a 59','60 a 64','65 a 69','70 a 74','75 a 79','80 y mas','Indeter']
data_2011 = pd.read_csv("2011.csv",header=0,names=col_2011) 
data_2012 = pd.read_csv("2012.csv",header=0,names=col_2012)
data_2013 = pd.read_csv("2013.csv",header=0,names=col_2013)
data_2014 = pd.read_csv("2014.csv",header=0,names=col_2014)

Как вы можете видеть, данные показывают регион, код смерти, причину смерти и возрастные диапазоны людей, зарегистрированных в каждой переписи. Вот мои фреймы данных (первые 5 строк)

CENSO 2011 ---------------------------------------------------------------------------------------
                  region    death_code                                 death_cause    sex      < 1  1 a 4  5 a 9  10 a 19  20 a 44  45 a 64  65 a 79  80 y mas
0  De Arica y Parinacota   A00-B99    Ciertas enfermedades infecciosas y parasitarias  Hombre    0      0      0        0        4        7        3         1
1  De Arica y Parinacota   A00-B99    Ciertas enfermedades infecciosas y parasitarias   Mujer    0      0      0        0        4        3        3         5
2  De Arica y Parinacota   C00-D48                               Tumores (neoplasias)  Hombre    0      0      0        0        9       41       67        41
3  De Arica y Parinacota   C00-D48                               Tumores (neoplasias)   Mujer    0      0      0        1        8       35       41        29
4  De Arica y Parinacota   E00-E90   Enfermedades endocrinas, nutricionales y meta...  Hombre    0      0      0        0        2        8       13        15

CENSO 2012 ---------------------------------------------------------------------------------------
                  region    death_code                                death_cause      sex       < 1  1 a 4  5 a 9  10 a 19  20 a 44  45 a 64  65 a 79  80 y mas
0  De Arica y Parinacota   A00-B99    Ciertas enfermedades infecciosas y parasitarias  Hombre    0      0      0        0        5        3        4         3
1  De Arica y Parinacota   A00-B99    Ciertas enfermedades infecciosas y parasitarias   Mujer    1      0      0        0        1        7        2         3
2  De Arica y Parinacota   C00-D48                               Tumores (neoplasias)  Hombre    0      0      0        0        6       39       58        37
3  De Arica y Parinacota   C00-D48                               Tumores (neoplasias)   Mujer    0      0      0        1       11       27       80        35
4  De Arica y Parinacota   E00-E90   Enfermedades endocrinas, nutricionales y meta...  Hombre    0      0      0        0        0       11       20         6
CENSO 2013 ---------------------------------------------------------------------------------------
                  region    death_code                                 death_cause            sex     0 a 4  ...  60 a 64  65 a 69  70 a 74  75 a 79  80 y mas
0  De Arica y Parinacota   A00-B99   Ciertas enfermedades infecciosas y parasitarias        Hombres      1  ...        1        1        2        2         9
1  De Arica y Parinacota   A00-B99   Ciertas enfermedades infecciosas y parasitarias        Mujeres      1  ...        0        1        2        1         5
2  De Arica y Parinacota   A00-B99   Ciertas enfermedades infecciosas y parasitarias  Indeterminado      0  ...        0        0        0        0         0
3  De Arica y Parinacota   C00-D48                              Tumores (neoplasias)        Hombres      0  ...       16       42       31       32        39
4  De Arica y Parinacota   C00-D48                              Tumores (neoplasias)        Mujeres      0  ...       12       23       13       19        40

CENSO 2014 ---------------------------------------------------------------------------------------
                  region     death_code      sex     0 a 4  5 a 9  10 a 14  15 a 19  20 a 24  ...  50 a 54  55 a 59  60 a 64  65 a 69  70 a 74  75 a 79  80 y mas  Indeter
0  De Arica y Parinacota   A00-B99        Hombres      0      0        0        0        0  ...        2        2        2        0        1        1         4        0
1  De Arica y Parinacota   A00-B99        Mujeres      1      0        0        0        0  ...        2        0        2        3        3        1         2        0
2  De Arica y Parinacota   A00-B99  Indeterminado      0      0        0        0        0  ...        0        0        0        0        0        0         0        0
3  De Arica y Parinacota   C00-D48        Hombres      0      0        0        1        0  ...        6       12       18       27       41       30        40        0
4  De Arica y Parinacota   C00-D48        Mujeres      0      0        0        1        0  ...       14       15       13       10       26       33        43        0

Дело в следующем: 1. Возрастные диапазоны разных фреймов данных различаются. 2. Фрейм данных census_2014 имеет неопределенный возрастной диапазон (люди, возраст которых невозможно узнать). 3. Фрейм данных census_2014 показывает только код смерти, но не причину смерти.

Как я могу правильно объединить эти фреймы данных, добавив столбец, показывающий соответствующий год? Я новичок в pandas и python. Спасибо, заранее и позаботьтесь.

...