How to conditionally fill missing values in a pandas DataFrame by group, stopping at new values?

==============================================================================

I am relatively new to the world of Python and currently in the process of learning how to effectively utilize its libraries and tools for data manipulation.
As I am still in the early stages of my Python journey, I am seeking advice and guidance on how to solve specific problems I encounter, particularly in handling missing data within a structured dataset.

=============================================================================

I am working with a pandas DataFrame that contains socio-economic indicators for various countries over several years (1960-2023). Each row corresponds to a country and a specific year, with columns for each indicator. Many of these columns have missing values which I need to fill based on the most recent non-missing value for each country. The fill should only continue until a new non-missing value is encountered, and it should restart with each new country.

Here’s a simplified example of what the DataFrame might look like:

Country Name | Year | Indicator1 | Indicator2 | Indicator3


Argentina | 2000 | 20 | NaN | NaN

Argentina | 2001 | NaN | NaN | 20

Argentina | 2002 | 40 | 30 | NaN

Brazil | 2000 | 15 | NaN | NaN

Brazil | 2001 | NaN | 20 | 10

I need the output to fill missing values based on the most recent previous entry per column, but each country’s data should be treated independently. Here’s the desired output:

Country Name | Year | Indicator1 | Indicator2 | Indicator3


Argentina | 2000 | 20 | 30 | 20

Argentina | 2001 | 20 | 30 | 20

Argentina | 2002 | 40 | 30 | 20

Brazil | 2000 | 15 | 20 | 10

Brazil | 2001 | 15 | 20 | 10

I have tried to implement a function in Python using pandas to fill missing values in a DataFrame containing socio-economic indicators for different countries over several years. The DataFrame is loaded from an Excel file, sorted by ‘Country Name’ and ‘Year’, and then I attempted to fill missing values forward within each country group.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
def conditional_fill_down(file_path):
data = pd.read_excel(file_path)
data_sorted = data.sort_values(by=['Country Name', 'Year'])
data_filled = data_sorted.groupby('Country Name').apply(lambda group: group.ffill()).reset_index(drop=True)
return data_filled
file_path = 'poverty.xlsx'
filled_data = conditional_fill_down(file_path)
output_path = 'poverty_edited.xlsx'
filled_data.to_excel(output_path, index=False)
</code>
<code>import pandas as pd def conditional_fill_down(file_path): data = pd.read_excel(file_path) data_sorted = data.sort_values(by=['Country Name', 'Year']) data_filled = data_sorted.groupby('Country Name').apply(lambda group: group.ffill()).reset_index(drop=True) return data_filled file_path = 'poverty.xlsx' filled_data = conditional_fill_down(file_path) output_path = 'poverty_edited.xlsx' filled_data.to_excel(output_path, index=False) </code>
import pandas as pd

def conditional_fill_down(file_path):
    data = pd.read_excel(file_path)
    
    data_sorted = data.sort_values(by=['Country Name', 'Year'])
    
    data_filled = data_sorted.groupby('Country Name').apply(lambda group: group.ffill()).reset_index(drop=True)
    
    return data_filled

file_path = 'poverty.xlsx'
filled_data = conditional_fill_down(file_path)

output_path = 'poverty_edited.xlsx'
filled_data.to_excel(output_path, index=False)

However, this approach didn’t work.

Could someone help me implement this in pandas in a way that the filling respects the boundaries of each country group and stops filling when a new non-missing value appears?

New contributor

David Gjeka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật