pd.merge не работает
Есть простенький код
import pandas as pd
relig = pd.read_excel('Religion in the United States by State and the District of Columbia (2014).xlsx')
birth = pd.read_excel('List of U.S. states and territories by birth and death rates in 2017.xlsx')
birth = birth.rename(columns={"state": "Region"})
relig = relig.replace(['<1.0', '<1,0'], 0)
pd.merge(relig, birth, how='outer')
И не смотря на одинаковость значений столбцов Region этих фреймов, получается такой результат
То есть он не видит общих значений между фреймами, а они есть.
Собственно сами файлы .xlsx: https://yadi.sk/d/iX5-RapA3zWQgA
Ответы (1 шт):
Автор решения: Smoooky
→ Ссылка
Дело в том что в таблице 'List of U.S. states and territories by birth and death rates in 2017.xlsx' названия штатов начинаются с пробелов. Для программы строки 'Arizona' и ' Arizona' не равны. Попробуйте так:
import pandas as pd
relig = pd.read_excel('Religion in the United States by State and the District of Columbia (2014).xlsx')
birth = pd.read_excel('List of U.S. states and territories by birth and death rates in 2017.xlsx')
birth = birth.rename(columns={"state": "Region"})
relig = relig.replace(['<1.0', '<1,0'], 0)
# Удаляем пробелы из строк с названием регионов в датафрейме birth
birth['Region'] = birth['Region'].map(lambda x: x[1:len(x)])
res = pd.merge(relig, birth, on='Region', how='outer')
