我已经阅读了类似的问题,但是它们似乎都比我的复杂得多,我无法将这些知识转移到我的案子中。
我有这个数据框('EUETS_ver`)
country year emissions iso2 sector
Austria 2005 33373156 AT regulated
Austria 2005 33373156 AT unregulated
Austria 2005 33373156 AT total
Belgium 2005 55363232 BE regulated
Belgium 2005 55363232 BE unregulated
Belgium 2005 55363232 BE total
Cyprus 2005 5078877 CY regulated
Cyprus 2005 5078877 CY unregulated
Cyprus 2005 5078877 CY total
我想用EUETS_ver$emissions
为EUETS$sector
的0替换unregulated
中的值。
我想要这个:
country year emissions iso2 sector
Austria 2005 33373156 AT regulated
Austria 2005 0 AT unregulated
Austria 2005 33373156 AT total
Belgium 2005 55363232 BE regulated
Belgium 2005 0 BE unregulated
Belgium 2005 55363232 BE total
Cyprus 2005 5078877 CY regulated
Cyprus 2005 0 CY unregulated
Cyprus 2005 5078877 CY total
我知道这应该很容易,但这对我来说不是...
预先感谢
nordsee
答案 0 :(得分:1)
>>> import pandas as pd
>>> countries_base = [{'Country': 'AU'}, {'Country': 'AL'}, {'Country': 'DZ'}]
>>> countries = [{"name":"Afghanistan","alpha-2":"AF","country-code":"004"},{"name":"Åland Islands","alpha-2":"AX","country-code":"248"},{"name":"Albania","alpha-2":"AL","country-code":"008"},{"name":"Algeria","alpha-2":"DZ","country-code":"012"}]
>>> df1 = pd.DataFrame(countries_base)
>>> df2 = pd.DataFrame(countries)
>>> m = pd.merge(df1, df2, how='inner', left_on='Country', right_on='alpha-2')
>>> if m.empty:
>>> print('Country code does not exist')
>>> else:
>>> print('Country code exists')